Hinzufügen einer Schnittstelle zu einem vorhandenen Server auf Fujitsu K5 oder OpenStack
2016-12-20
Machine-translated — the English original is authoritative.
Heute hatte ich eine Anfrage von einem Kunden, der nach Hinweisen suchte, wie man eine weitere Schnittstelle zu einer vorhandenen K5- oder OpenStack-Instanz hinzufügt. Ich hoffe, wenn Sie diesen Blog gefunden haben, können auch Sie ihn als nützlich erachten. Hier wird die große Annahme getroffen, dass Sie Ihre Umgebung für die Verwendung mit Python 2.7X eingerichtet haben. Da ich jedoch die nativen API-Aufrufe verwendet habe, sollte es sehr einfach sein, dieses Beispiel auf andere Skriptsprachen zu portieren, sofern diese RESTful-Aufrufe unterstützen.
Hinweis: Beim Ausführen des Skripts in einer Windows-Betriebssystemumgebung kann es erforderlich sein, die einfachen Anführungszeichen (‘) durch doppelte Anführungszeichen (“) um die Parameter herum zu ersetzen.
Dieses Verfahren erfordert nur zwei API-Aufrufe –
Wie Sie den obigen Links entnehmen können, habe ich die nativen OpenStack-API-Aufrufe aus der Dokumentation der Keystone- und Nova-Entwickler-APIs verwendet. Fujitsu K5 hat einige dieser APIs ebenfalls erweitert, um reichhaltigere Funktionen bereitzustellen. Diese Details sind auf der Fujitsu K5-Website dokumentiert – http://www.fujitsu.com/uk/solutions/cloud/k5/guides/
Enthalten ist ein grundlegender Wrapper um die API-Aufrufe, um die einfache Verwendung des Python-Skripts über die Befehlszeile zu erleichtern. Das Skript benötigt 3 Eingabeparameter –
-p 'project id'– dies ist die ID des Projekts, in dem sich der Server befindet-n 'network id'– dies ist die ID des Netzwerks, an das die neue Schnittstelle angehängt werden soll-s 'server id'– wie der Name schon sagt, befindet sich hier die ID des Servers, der die zusätzliche Schnittstelle benötigt
Beachten Sie auch, dass ich die K5-Vertragsdetails (z. B. Benutzername, Passwort usw.) nicht hart in das Skript codiere, sondern sie aus einer anderen Datei namens k5contractsettingsV7.py importiere, die SIE VOR DEM AUSFÜHREN DES SKRIPTS ERSTELLEN MÜSSEN. Legen Sie diese Datei im selben Verzeichnis wie das unten stehende Skript ab.
Beispiel für die Ausführung des Skripts –
~/Dev/K5/CustomerAPIExamples$ ./ExampleServerAPIv1.py
Script to demonstrate how to add an interface to an existing server.
Once complete it will be necessary to configure the interface within the OS (a reboot may be required)
Usage: ./ExampleServerAPIv1.py -n 'network id' -s 'server id' -p 'project id'
~/Dev/K5/CustomerAPIExamples$ ./ExampleServerAPIv1.py -n 'b514ab88-0a32-4b84-a73f-d5eabfd9de72' -p '7015d1478a4c4bd7b970215d7b0260dd' -s '8a33075f-0894-4b9b-8b16-047457952f74'
8cee65fa81a44c4e9f5f111e6baea420
{u'interfaceAttachment': {u'port_state': u'DOWN', u'port_id': u'f645038a-2744-4c92-b0a6-2d350086a9ed', u'fixed_ips': [{u'subnet_id': u'1588ed79-3efe-44a7-be5b-075eb653f3bd', u'ip_address': u'192.168.1.6'}], u'net_id': u'b514ab88-0a32-4b84-a73f-d5eabfd9de72', u'mac_addr': u'fa:16:3e:5f:77:07'}}
Beispiel-Python-Skript zum Hinzufügen einer neuen Netzwerkschnittstelle zu einer vorhandenen K5- oder OpenStack-Instanz.
Diese Datei enthält versteckte oder bidirektionale Unicode-Zeichen, die anders interpretiert oder kompiliert werden können als das, was unten angezeigt wird. Um sie zu überprüfen, öffnen Sie die Datei in einem Editor, der versteckte Unicode-Zeichen anzeigt.
Mehr über bidirektionale Unicode-Zeichen erfahren
#!/usr/bin/python |
|
"""Summary: Python 2.7X API example calls to do the following |
|
1. Get a project scoped K5 token |
|
2. Attach a new interface to an existing server |
|
|
|
Prerequisites: k5contractsettingsV7.py file in the same directory with K5 contract login details |
|
|
|
adminUser = 'username' |
|
adminPassword = 'password' |
|
contract = 'contract\_name' |
|
contractid = 'contract\_id' |
|
defaultid = 'default\_project\_id' |
|
region = 'uk-1' |
|
|
|
Author: Graham Land |
|
Date: 20/12/16 |
|
Twitter: @allthingsclowd |
|
Github: <https://github.com/allthingsclowd> |
|
Blog: <https://allthingscloud.eu> |
|
|
|
|
|
""" |
|
|
|
import requests |
|
import getopt |
|
import sys |
|
from k5contractsettingsV7 import * |
|
|
|
|
|
def get_scoped_token(adminUser, adminPassword, contract, projectid, region): |
|
"""Summary – Get a regional project scoped token using a username and password |
|
|
|
Returns: |
|
Object: Regionally Scoped Project Token Object |
|
|
|
Args: |
|
adminUser (TYPE): username |
|
adminPassword (TYPE): password |
|
contract (TYPE): contract name |
|
projectid (TYPE): project id |
|
region (TYPE): region |
|
""" |
|
identityURL = 'https://identity.' + region + \ |
|
.cloud.global.fujitsu.com/v3/auth/tokens' |
|
|
|
try: |
|
response = requests.post(identityURL, \ |
|
headers={'Content-Type': 'application/json', \ |
|
'Accept': 'application/json'}, \ |
|
json={"auth": \ |
|
{"identity": \ |
|
{"methods": ["password"], "password": \ |
|
{"user": \ |
|
{"domain": \ |
|
{"name": contract}, \ |
|
"name": adminUser, \ |
|
"password": adminPassword \ |
|
}}}, \ |
|
"scope": \ |
|
{"project": \ |
|
{"id": projectid \ |
|
}}}) |
|
|
|
return response |
|
except: |
|
return 'Regional Project Token Scoping Failure' |
|
|
|
|
|
def attach_interface_to_server(k5token, project_id, net_id, server_id, region): |
|
"""Summary |
|
|
|
Args: |
|
k5token (TYPE): project scoped token |
|
project_id (TYPE): id of the project where the server resides |
|
net_id (TYPE): network id of the new interface to be added to the server |
|
server_id (TYPE): id of the server to receive the new interface |
|
region (TYPE): K5 region |
|
|
|
Returns: |
|
TYPE: Description |
|
""" |
|
|
|
serverURL = 'https://compute.' + region + '.cloud.global.fujitsu.com/v2/' + \ |
|
project_id + '/servers/' + server_id + '/os-interface' |
|
try: |
|
response = requests.post(serverURL, \ |
|
headers={ \ |
|
'X-Auth-Token': k5token, 'Content-Type': 'application/json', 'Accept': 'application/json'}, \ |
|
json={"interfaceAttachment": \ |
|
{"net_id": net_id \ |
|
}}) |
|
return response |
|
except: |
|
return "Unable to add interface to server" |
|
|
|
|
|
def main(): |
|
try: |
|
|
|
# ensure minimium commandline paramaters have been supplied |
|
if (len(sys.argv)<6): |
|
print("Script to demonstrate how to add an interface to an existing server.") |
|
print("Once complete it will be necessary to configure the interface within the OS (a reboot may be required)") |
|
print("Usage: %s -n 'network id' -s 'server id' -p 'project id'" % sys.argv[0]) |
|
sys.exit(2) |
|
|
|
|
|
# load the command line parameters |
|
myopts, args = getopt.getopt(sys.argv[1:],"n:p:s:",["net_id=","project_id=","server_id="]) |
|
except getopt.GetoptError: |
|
# if the parameters are incorrect display error message |
|
print("Script to demonstrate how to add an interface to an existing server.") |
|
print("Once complete it will be necessary to configure the interface within the OS (a reboot may be required)") |
|
print("Usage: %s -n 'network id' -s 'server id' -p 'project id'" % sys.argv[0]) |
|
sys.exit(2) |
|
|
|
# set the variables from the command line parameters |
|
|
|
############################# |
|
# o == option |
|
# a == argument passed to the o |
|
############################# |
|
for o, a in myopts: |
|
if o in ('-s', '--server_id'): |
|
server_id = a |
|
elif o in ('-n', '--net_id'): |
|
network_id = a |
|
elif o in ('-p', '--projects'): |
|
project_id = a |
|
else: |
|
# if the parameters are incorrect display error message |
|
print("Script to demonstrate how to add an interface to an existing server.") |
|
print("Once complete it will be necessary to configure the interface within the OS (a reboot may be required)") |
|
print("Usage: %s -n 'network id' -s 'server id' -p 'project id'" % sys.argv[0]) |
|
|
|
k5token = get_scoped_token(adminUser, adminPassword, contract, project_id, region).headers['X-Subject-Token'] |
|
print k5token |
|
result = attach_interface_to_server(k5token, project_id, network_id, server_id, region) |
|
print result.json() |
|
|
|
if __name__ == "__main__": |
|
main() |
|
|
view raw
K5_Add_Interface_To_Existing_Server.py
hosted with ❤ by GitHub
Bitte beachten Sie auch, dass Sie nach Abschluss dieses Schritts nur den Infrastrukturteil des Prozesses erfüllt haben. Es wird immer noch notwendig sein, die neue Schnittstelle im jeweiligen Betriebssystem zu konfigurieren.
Happy Stacking!
withk5youcan
Originally published on allthingscloud.eu (2016-12-20).