In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the Newton version of Openstack how to achieve Dashboard development, the article is very detailed, has a certain reference value, interested friends must read it!
Introduction to Dashboard
Openstack's Dashboard project is generally known as Horizon. According to the simple installation and deployment of the official website, yum install openstack-dashboard can be easily configured, as long as you modify / etc/openstack-dashboard/local_settings and / etc/sysconfig/memcached, and then start the httpd service to log in.
But if you look at its source code, you will find that it is not as simple as you think.
Horizon: it is a standard python wsgi program based on django webframework and generally runs on webserver (apache httpd).
The source code of Horizon contains two code folders
Horizon # / usr/lib/python2.7/site-packages/horizon
Openstack_dashboard # / usr/share/openstack-dashboard
Create a Dashboard1. Environment preparation operating system Centos7.2OpenstackNewtonPython2.7.5Django1.8.14
View Django version
# pythonPython 2.7.5 (default, Nov 6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2Type "help", "copyright", "credits" or "license" for more information. > > import django > print django.VERSION (1,8,14, 'final', 0) 2. Create a dashboard application
Since each dashboard is an app in django, you can use the django command to create a dashboard directory structure
# cd / usr/share/openstack-dashboard# python manage.pyType 'manage.py help' for help on a specific subcommand.Available subcommands: [auth] changepassword createsuperuser [compressor] compress mtime_ Cache [Django] check compilemessages createcachetable dbshell diffsettings dumpdata flush inspectdb loaddata makemessages makemigrations migrate runfcgi shell showmigrations sql sqlall sqlclear sqlcustom sqldropindexes sqlflush sqlindexes sqlmigrate sqlsequencereset squashmigrations startapp startproject syncdb test testserver validate [horizon] Startdash startpanel[openstack _ dashboard] make_web_conf migrate_ settings[sessions] clearsessions [staticfiles] collectstatic findstatic runserver
You can see that there are two commands startdash and startpanel in the horizon section, which can be used directly:
# mkdir-p openstack_dashboard/dashboards/dlwdashboard# python manage.py startdash dlwdashboard-- target openstack_dashboard/dashboards/dlwdashboard# mkdir-p openstack_dashboard/dashboards/dlwdashboard/dlwpanel# python manage.py startpanel dlwpanel-- dashboard=openstack_dashboard.dashboards.dlwdashboard-- target=openstack_dashboard/dashboards/dlwdashboard/dlwpanel2.1, Directory structure: # cd openstack_dashboard/dashboards/# tree dlwdashboard/dlwdashboard/ ├── dashboard.py ├── dashboard.pyc ├── dlwpanel │ ├── _ _ init__.py │ ├── panel.py │ ├── templates │ │ └── dlwpanel │ │ └── index.html │ ├── tests.py │ ├── urls.py views.py _ _ init__.py ├── _ _ init__.pyc ├── static │ └── dlwdashboard │ ├── js │ │ └── dlwdashboard.js │ └── scss │ └── dlwdashboard.scss └── templates └── dlwdashboard └── base.html9 directories 13 files2.2 defines a dashboard dashboard# cd dlwdashboard/# vi dashboard.py# Licensed under the Apache License, Version 2.0 (the "License") You may# not use this file except in compliance with the License. You may obtain# a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "ASIS" BASIS, WITHOUT# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the# License for the specific language governing permissions and limitations# under the License.from django.utils.translation import ugettext_lazy as _ import horizonclass Dlwgroup (horizon.PanelGroup): slug = "dlwgroup" name = _ ("Dlw Group") panels = ('dlwpanel',) class Dlwdashboard (horizon.Dashboard): # name = _ ("Dlwdashboard") name = _ ("Dlw") slug = "dlwdashboard" panels = (Dlwgroup,) # Add your panels here. Default_panel = 'dlwpanel' # Specify the slug of the dashboard's default panel.horizon.register (Dlwdashboard)
You can define and modify the name of the dashboard
Name = _ ("Dlw") create panel panel3.1, directory structure # tree dlwpanel/dlwpanel/ ├── _ _ init__.py ├── panel.py ├── templates │ └── dlwpanel │ index.html ├── tests.py ├── urls.py └── views.py2 directories, 6 files3.2, definition panel # cd dlwpanel# vi panel.py # Licensed under the Apache License, Version 2.0 (the "License") You may# not use this file except in compliance with the License. You may obtain# a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "ASIS" BASIS, WITHOUT# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the# License for the specific language governing permissions and limitations# under the License.from django.utils.translation import ugettext_lazy as _ import horizonfrom openstack_dashboard.dashboards.dlwdashboard import dashboardclass Dlwpanel (horizon.Panel): name = _ ("Dlwpanel") slug = "dlwpanel" dashboard.Dlwdashboard.register (Dlwpanel) 3.3, Define the form tables# vi tables.pyfrom django.utils.translation import ugettext_lazy as _ from horizon import tablesclass MyFilterAction (tables.FilterAction): name = "myfilter" class InstancesTable (tables.DataTable): name = tables.Column ('name') \ verbose_name=_ ("Name") status = tables.Column ('status',\ verbose_name=_ ("Status")) zone = tables.Column (' availability_zone',\ verbose_name=_ ("Availability Zone")) image_name = tables.Column ('image_name' \ verbose_name=_ ("Image Name") class Meta (object): name= "instances" verbose_name=_ ("Instances") table_actions = (MyFilterAction) Definition tag page tab# vi tab.pyfrom django.utils.translation import ugettext_lazy as_ from horizon import exceptionsfrom horizon import tabsfrom openstack_dashboard import apifrom openstack_dashboard.dashboards.dlwdashboard.dlwpanel import tablesclass InstanceTab (tabs.TableTab): name = _ ("InstancesTab") slug = "instances_tab" table_classes = (tables.InstancesTable,) template_name = ("horizon/common/_detail_table.html") preload = False def has_more_data (self) Table): return self._has_more def get_instances_data (self): try: marker = self.request.GET.get (tables.InstancesTable._meta.pagination_param, None) instances, self._has_more = api.nova.server_list (self.request, search_opts= {'marker': marker) 'paginate': True}) return instances except Exception: self._has_more = False error_message = _ (' Unable to get instances') exceptions.handle (self.request, error_message) return [] class DlwpanelTabs (tabs.TabGroup): slug = "dlwpanel_tabs" tabs = (InstanceTab ) sticky = True3.4, definition view views.py# vi views.py# Licensed under the Apache License, Version 2.0 (the "License") You may# not use this file except in compliance with the License. You may obtain# a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "ASIS" BASIS, WITHOUT# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the# License for the specific language governing permissions and limitations# under the License.from horizon import tabsfrom openstack_dashboard.dashboards.dlwdashboard.dlwpanel\ import tabs as dlwdashboard_tabsclass IndexView (tabs.TabbedTableView): tab_group_class = dlwdashboard_tabs.DlwpanelTabs template_name = 'dlwdashboard/dlwpanel/index.html' def get_data (self, request, context, * args, * * kwargs): # Add data to the context here... Return context3.5, automatically generated url.py# vi url.py# Licensed under the Apache License, Version 2.0 (the "License"); you may# not use this file except in compliance with the License. You may obtain# a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "ASIS" BASIS, WITHOUT# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the# License for the specific language governing permissions and limitations# under the License.from django.conf.urls import urlfrom openstack_dashboard.dashboards.dlwdashboard.dlwpanel import viewsurlpatterns = [url (r'^ $', views.IndexView.as_view (), name='index') ] 3.6 、 Define template template# cd templates/dlwpanel/# vi index.html {% extends' base.html'%} {% load i18n%} {% block title%} {% trans "DlwPanel"%} {% endblock%} {% block page_header%} {% include "horizon/common/_page_header.html" with title=_ ("DlwPanel")%} {% endblock page_header%} {% block main%} {% tab_group.render} {% endblock% } activate the new dashboard# cd / usr/share/openstack-dashboard/openstack_dashboard/enabled# vi _ 50_dlwdashboard.py # The name of the dashboard to be added to HORIZON ['dashboards']. Required.DASHBOARD = 'dlwdashboard'# If set to True, this dashboard will not be added to the settings.DISABLED = False# A list of applications to be added to INSTALLED_APPS.ADD_INSTALLED_APPS = [' openstack_dashboard.dashboards.dlwdashboard',]
Restart the httpd service
Page view
Redefine the form tables
Add an ip and add the ip code to the tables.py defined above, which can be extracted directly from the original openstack code (because the name,status,zone,image_name here is picked)
# cat tables.py from django.utils.translation import ugettext_lazy as _ from horizon import tablesfrom django import templateclass MyFilterAction (tables.FilterAction): name = "myfilter" import sixdef get_ips (instance): template_name = 'project/instances/_instance_ips.html' ip_groups = {} for ip_group Addresses in six.iteritems (instance.addresses): ip_ groups [IP _ group] = {} ip_ groups [IP _ group] ["floating"] = [] ip_ groups [IP _ group] ["non_floating"] = [] for address in addresses: if ('OS-EXT-IPS:type' in address and address [' OS-EXT-IPS:type'] = = "floating") ): ip_ groups [IP _ group] ["floating"] .append (address) else: ip_ groups [IP _ group] ["non_floating"] .append (address) context = {"ip_groups": ip_groups } return template.loader.render_to_string (template_name, context) class InstancesTable (tables.DataTable): name= tables.Column ('name',\ verbose_name=_ ("Name")) status = tables.Column (' status',\ verbose_name=_ ("Status")) zone = tables.Column ('availability_zone' \ verbose_name=_ ("Availability Zone") image_name = tables.Column ('image_name',\ verbose_name=_ ("Image Name")) ip = tables.Column (get_ips, verbose_name=_ ("IP Address")) Attrs= {'data-type': "ip"}) class Meta (object): name = "instances" verbose_name = _ ("Instances") table_actions = (MyFilterAction,)
Restart the httpd service page to view
Summary
To create a new dashboard, here is the whole process of group and panel panel:
1.app2.dashboard.py3.panel.py4.tables.py5.tabs.py6.views7.url.py8.template9.enabled10.httpd
The design of the Horizon panel is divided into three layers: Dashboard → PanelGroup → Panel
There are four existing dashboard in Horizon:
1. Items panel seen by ordinary project users after login. 2. Admin management is visible after login, left administrator panel 3. Settings upper right corner settings panel, which can set language, time zone, change password 4. Router (open profile_support visible in configuration file), ciso nexus 1000V management panel
Each dashboard is an app,django in django. App in app can be understood as a means to modularize business logic. It can contain its own unique url settings, templates, and business logic code.
A series of PanelGroup is defined under each dashboard, and virtual machine management corresponds to a PanelGroup (Manage Compute) on the interface, in which there are a series of sub-panel (Overview, Instances, Volumes …) . The management panel of Swift,heat,neutron is each a PanelGroup, with its own sub-panel.
Create a panel under an existing dashboard
Here you choose to build a blank panel called dbc under project, and with the above steps, it is very simple to build a blank panel in three steps.
1. Establish panel# / usr/share/openstack-dashboard/openstack_dashboard/dashboards/project/# mkdir bdc# cd / usr/share/openstack-dashboard/# python manage.py startpanel bdc-- dashboard=openstack_dashboard.dashboards.project-- target=openstack_dashboard/dashboards/project/bdc2. Activate enabled# cd / usr/share/openstack-dashboard/openstack_dashboard/enabled# cp _ 1050_project_images_panel.py _ 1051_project_bdc_panel.py # vi _ 1051_project_bdc_panel.py # (c) Copyright 2016 Hewlett Packard Enterprise Development Company LP## Licensed under the Apache License, Version 2.0 (the "License"); you may# not use this file except in compliance with the License. You may obtain# a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "ASIS" BASIS, WITHOUT# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the# License for the specific language governing permissions and limitations# under the License.# The slug of the panel to be added to HORIZON_CONFIG. Required.PANEL = 'bdc'# The slug of the dashboard the PANEL associated with. Required.PANEL_DASHBOARD = 'project'# The slug of the panel group the PANEL is associated with.PANEL_GROUP =' compute'# Python panel class of the PANEL to be added.ADD_PANEL = 'openstack_dashboard.dashboards.project.bdc.panel.Bdc'
3. Restart httpd
# systemctl restart httpd
Page view
Internationalization 1. Brief introduction
Internationalization, a component of django, supports multiple languages. Pages opened by different machines display different languages. Here is a simple transformation of the relationship between Chinese and English. There is a django.mo file in the / usr/share/openstack-dashboard/openstack_dashboard/locale/zh_CN/LC_MESSAGES directory, which is used as the basis for Chinese-English translation. Take it out and put it in the G:/Python activity directory:
Sftp > cd / usr/share/openstack-dashboard/openstack_dashboard/locale/zh_CN/LC_MESSAGESsftp > lcd G:/Python practice sftp > get django.mo2. Decompiled django.mo
Mo files cannot be edited and need to be decompiled into .po files
Install a tool software, Poedit
Decompilation using cmd or powershell
The command line goes to the installation directory of Poedit
Cd E:\ Program Files (x86)\ Poedit\ GettextTools\ binmsgunfmt.exe G:\ Python exercise\ django.mo-o G:\ Python exercise\ django.po3. Modify the po file and compile
To generate a django.po file, you can use the Pycharm tool to open the po file and follow the new Bdc panel created before Hulu's translation.
Use Poedit to compile a po file to a mo file
Upload to / usr/share/openstack-dashboard/openstack_dashboard/locale/zh_CN/LC_MESSAGES
Restart the httpd service
The above is all the contents of the article "how to achieve Dashboard Development in Openstack for Newton". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.