- Infrastrukturní DevOps s HPE OneView (1) – Infrastructure as Code
- Infrastrukturní DevOps s HPE OneView (2) – API
- Infrastrukturní DevOps s HPE OneView (3) – Message bus
- Infrastrukturní DevOps s HPE OneView (4) – PowerShell
- Infrastrukturní DevOps s HPE OneView (5) – PowerShell skripty
- Infrastrukturní DevOps s HPE OneView (6) – Python
- Infrastrukturní DevOps s HPE OneView (7) – Python skripty
- Infrastrukturní DevOps s HPE OneView (8) – vaše vlastní aplikace s Grommet
- Infrastrukturní DevOps s HPE OneView (9) – Ansible a infrastruktura
- Infrastrukturní DevOps s HPE OneView (10) – Ansible a Blade networking
- Infrastrukturní DevOps s HPE OneView (11) – Ansible a síťový fabric
- Infrastrukturní DevOps s HPE OneView (12) – Ansible a servery
Přístup k API příjemný pro aplikace, ale ne pro lidi. Posuňme se teď o jednu úroveň nahoru a využijme knihoven do skriptovacích a programovacích jazyků. Ovládání OneView pro nás bude podstatně jednodušší.
Knihovny do programovacích a skriptovacích jazyků
Abychom nemuseli skládat svoje vlastní API volání a hrabat s v útrobách URI pro jednotlivé příkazy a montování JSON objektů, můžeme použít hotové knihovny do programovacích a skriptovacích jazyků, které velkou část práce udělají za nás. K dispozici je PowerShell, Python, Ruby a Golang. Dnes se podíváme na interaktivní ovládání přes PowerShell, příště už si napíšem nějaké užitečnější PowerShell skripty a pak se přesuneme k Python.
PowerShell a OneView v interaktivním režimu
Ještě než se poustíme do nějakých skriptů, můžeme si s PowerShell knihovnou pohrát v interaktivním režimu, tedy zadávat příkazy, které se budou rovnou vykonávat.
Nejprve ve svém Windows stroji nainstalujte knihovnu pro PowerShell. Je distribuována jako exe soubor, který jednoduše spustíte. Nejnovější stáhnete zde:
https://github.com/HewlettPackard/POSH-HPOneView/releases/
Ve svém počítači spusťte PowerShell a nátáhněte si OneView knihovnu.
PS C:\> Import-Module HPOneView.200 Welcome to the HP OneView POSH Library, v2.0.604.0 ----------------------------------------------------- To get a list of available CMDLETs in this library, type : Get-Command -module HPOneView.200 To get help for a specific command, type: get-help [verb]-HPOV[noun] To get extended help for a specific command, type: get-help [verb]-HPOV[noun] -full To update the offline help for this module, type: Update-Help -module HPOneView.200 Module sample scripts are located at: C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\HPOneView.200\Samples If you need further help, please consult one of the following: • Get-Help about_HPOneView.200 • Get-Help about_Appliance_Connections • Online documentation at https://github.com/HewlettPackard/POSH-HPOneView/wiki • Online Issues Tracker at https://github.com/HewlettPackard/POSH-HPOneView/issues (C) Copyright 2013-2016 Hewlett Packard Enterprise Development LP Note: Set your PowerShell console width to 150 to properly view report output. (Current Max Width: 120)
Knihovna nám dobře radí jak se můžeme dostat k nápovědě, aniž bychom museli hledat někde na webu. Seznam možných příkazů dostanete takto (výpis jsem zkrátil):
PS C:\> Get-Command -module HPOneView.200 CommandType Name Version Source ----------- ---- ------- ------ Function Add-HPOVBaseline 2.0.604.0 HPOneView.200 ... Function Get-HPOVEnclosure 2.0.604.0 HPOneView.200 Function Get-HPOVEnclosureGroup 2.0.604.0 HPOneView.200 ... Function Get-HPOVHealthStatus 2.0.604.0 HPOneView.200 ... Function Set-HPOVNetwork 2.0.604.0 HPOneView.200 Function Set-HPOVNetworkSet 2.0.604.0 HPOneView.200 ... Function Set-HPOVServerPower 2.0.604.0 HPOneView.200 Function Set-HPOVSMTPConfig 2.0.604.0 HPOneView.200 Function Set-HPOVSnmpReadCommunity 2.0.604.0 HPOneView.200 Function Set-HPOVStorageVolume 2.0.604.0 HPOneView.200 Function Set-HPOVStorageVolumeTemplatePolicy 2.0.604.0 HPOneView.200 ... Function Show-HPOVFirmwareReport 2.0.604.0 HPOneView.200 Function Show-HPOVLogicalInterconnectMacTable 2.0.604.0 HPOneView.200 Function Show-HPOVPortStatistics 2.0.604.0 HPOneView.200 Function Show-HPOVSanEndpoint 2.0.604.0 HPOneView.200 Function Show-HPOVUserSession 2.0.604.0 HPOneView.200 Function Show-HPOVUtilization 2.0.604.0 HPOneView.200 Function Start-HPOVLibraryTrace 2.0.604.0 HPOneView.200 Function Start-HPOVServer 2.0.604.0 HPOneView.200 Function Stop-HPOVAppliance 2.0.604.0 HPOneView.200 Function Stop-HPOVLibraryTrace 2.0.604.0 HPOneView.200 Function Stop-HPOVServer 2.0.604.0 HPOneView.200 ...
Můžeme požádat o pomoc pro jeden konkrétní příkaz, například ten, co nás připojí do OneView appliance.
PS C:\> get-help Connect-HPOVMgmt NAME Connect-HPOVMgmt SYNOPSIS Initiate a connection to an HPE OneView appliance. SYNTAX Connect-HPOVMgmt [-Hostname] <String> [-UserName] <String> [-Password] <String> [-AuthLoginDomain] <String> [-LoginAcknowledge [<SwitchParameter>]] [<CommonParameters>] DESCRIPTION Establish a connection with the specified HPE OneView appliance. Logs the user into the appliance and establishes a session for use with subsequent requests. Prompts will be displayed for any omitted values. Appliance hostname or IP can include an alternate TCP port number. While the appliance does not allow the default TCP port 443 to be changed, the appliance could reside behind a firewall, which is redirecting an alternate TCP port number. RELATED LINKS Disconnect-HPOVMgmt Start-HPOVLibraryTrace Stop-HPOVLibraryTrace Online Version: https://github.com/HewlettPackard/POSH-HPOneView/wiki/Connect-HPOVMgmt REMARKS To see the examples, type: "get-help Connect-HPOVMgmt -examples". For more information, type: "get-help Connect-HPOVMgmt -detailed". For technical information, type: "get-help Connect-HPOVMgmt -full". For online help, type: "get-help Connect-HPOVMgmt -online"
Jde o zkrácenou podobu, přepínačem examples dostanete příklady použítí, přepínačem full pak úplně všechno.
PS C:\> get-help Connect-HPOVMgmt -examples NAME Connect-HPOVMgmt SYNOPSIS Initiate a connection to an HPE OneView appliance. -------------------------- EXAMPLE 1 -------------------------- PS C:\> Connect-HPOVMgmt -appliance myappliance.domain.com ConnectionID Name UserName AuthLoginDomain ------------ ---- -------- --------------- 1 myappliance.domain.com Administrator LOCAL Connect to a specific appliance FQDN. The user will be prompted for authentication provider, user name and password. -------------------------- EXAMPLE 2 -------------------------- PS C:\> Connect-HPOVMgmt -appliance myappliance.domain.com:11223 Connect to a specific appliance, where the target TCP port isn't the default. The user will be prompted for authentication provider, user name and password.
Začněme tedy tím, že se připojíme do OneView appliance (výpis jsem zkrátil):
PS C:\> Connect-HPOVMgmt -Hostname 192.168.89.100 -UserName Administrator -Password HPEnet123 Certificate : [Subject] CN=oneview.local, O=Hewlett-Packard, L=Palo Alto, S=California, C=US ... ConnectionID Name UserName AuthLoginDomain Default Connection ------------ ---- -------- --------------- ------------------ 1 192.168.89.100 Administrator LOCAL True
Pojďme použít příkaz Get-HPOVAlert, kterým získáme seznam všech alertů. Ale když to uděláte, bude toho na obrazovce trochu moc, tak tento výpis pojďme raději uložit do proměnné.
$alerts = Get-HPOVAlert
Teď se můžeme podívat třeba jen na první a druhý záznam – takhle:
PS C:\> $alerts[0,1] Severity : OK Description : Flex NIC subport state on Encl1, interconnect 1, port d11 changed. Resource : Encl1, interconnect 1 Date : Thu Aug 11 09:40:32 2016 State : Cleared Owner : Notes : [8/11/2016 9:40:32 AM] Alert cleared. Severity : OK Description : Flex NIC subport state on Encl1, interconnect 1, port d11 changed. Resource : Encl1, interconnect 1 Date : Thu Aug 11 08:33:34 2016 State : Cleared Owner : Notes : [8/11/2016 8:33:34 AM] Alert cleared.
Interaktivní režim nám ukazuje alerty v textové podobě, ale můžeme se podívat, jak vypadají ve skutečnosti (jde o JSON, ve kterém je podstatně víc detailů).
PS C:\> Write-Host $alerts[0] @{type=AlertResourceV3; serviceEventDetails=; parentAlert=; childAlerts=System.Object[]; serviceEventSource=False; resou rceUri=/rest/interconnects/7aa38ef4-76bc-463c-aca0-db2978b62da5; associatedResource=; physicalResourceType=interconnects ; alertState=Cleared; severity=OK; eTag=2016-08-11T07:40:32.808Z; created=2016-08-11T07:40:32.808Z; modified=2016-08-11T 07:40:32.808Z; alertTypeID=swmon.subPortStatusChange; clearedTime=2016-08-11T07:40:32.808Z; correctiveAction=; healthCat egory=InterconnectBay; urgency=None; lifeCycle=False; activityUri=; resourceID=; associatedEventUris=System.Object[]; as signedToUser=; changeLog=System.Object[]; clearedByUser=System; category=alerts; description=Flex NIC subport state on E ncl1, interconnect 1, port d11 changed.; uri=/rest/alerts/671; ApplianceConnection=}
V PowerShell se automaticky takové objekty parsují, takže si můžeme třeba vypsat jen description alertu.
PS C:\> Write-Host $alerts[0].description Flex NIC subport state on Encl1, interconnect 1, port d11 changed.
Na závěr si ukažme, že PowerShell je podstatně mocnější a podporuje vytváření cyklů apod. Co kdybychom chtěli vypsat všechny alerty, ale textově tak, že na řádku bude jen čas vytvoření a text hlášky? Nejprve získáme výpis a přes symbol pipe ho pošleme do cyklu. Ten bude procházet každý objekt (reprezentovaný proměnnou $_), tedy v našem případě položky v seznamu alertů, a u každého zobrazí čas vytvoření a jeho text. Zkuste si to:
PS C:\> Get-HPOVAlert | ForEach-Object { >> Write-Host $_.created " " $_.description >> } 2016-08-11T07:40:32.808Z Flex NIC subport state on Encl1, interconnect 1, port d11 changed. 2016-08-11T06:33:34.513Z Flex NIC subport state on Encl1, interconnect 1, port d11 changed. 2016-08-11T06:33:23.031Z Connection deployed on downlink port 11, subport a. Connection enforcement succeeded. 2016-08-11T06:32:22.890Z For uplink set DEV-uplink, all uplink ports are operational. 2016-08-11T06:32:22.540Z The stacking health has changed to redundantly connected. 2016-08-11T06:32:22.183Z The logical enclosure is consistent with its enclosure group {"name":"ENCL-group", "uri":"/rest/enclosure-groups/ac26c12c-6b91-49d7-b73c-e6c832a64a4f"}. 2016-08-11T06:32:21.480Z The logical interconnect is consistent with the logical interconnect group FlexFabric. 2016-08-11T06:31:54.163Z For uplink set DEV-uplink, all uplink ports are operational. 2016-08-11T06:31:52.878Z The logical enclosure is inconsistent with its enclosure group {"name":"ENCL-group", "uri":"/rest/enclosure-groups/ac26c12c-6b91-49d7-b73c-e6c832a64a4f"}. 2016-08-11T06:31:52.265Z The logical interconnect is inconsistent with the logical interconnect group FlexFabric. 2016-08-11T06:24:00.617Z Cold Start. 2016-08-11T06:24:00.530Z The server has been powered on. 2016-08-11T06:23:37.340Z The server has been powered off. 2016-08-11T06:20:35.427Z The server has been powered on. 2016-08-11T06:20:35.358Z Cold Start. 2016-08-11T05:27:09.682Z Flex NIC subport state on Encl1, interconnect 1, port d13 changed. 2016-08-11T05:23:17.208Z Flex NIC subport state on Encl1, interconnect 1, port d12 changed. 2016-08-11T05:02:50.732Z The server has been powered off. 2016-08-11T05:02:45.822Z Flex NIC subport state on Encl1, interconnect 1, port d11 changed. 2016-08-11T05:01:56.328Z The server has been powered off. 2016-08-11T04:59:55.524Z The server has been powered off. 2016-08-11T04:58:02.280Z Flex NIC subport state on Encl1, interconnect 1, port d13 changed. 2016-08-11T04:57:42.254Z Cold Start. 2016-08-11T04:57:42.217Z The server has been powered on. 2016-08-11T04:57:18.068Z Connection deployed on downlink port 13, subport a. Connection enforcement succeeded. 2016-08-11T04:57:07.732Z Flex NIC subport state on Encl1, interconnect 1, port d12 changed. 2016-08-11T04:56:47.678Z Cold Start. 2016-08-11T04:56:47.632Z The server has been powered on. 2016-08-11T04:56:22.697Z Connection deployed on downlink port 12, subport a. Connection enforcement succeeded. 2016-08-11T04:55:21.367Z Flex NIC subport state on Encl1, interconnect 1, port d11 changed. 2016-08-11T04:54:46.994Z The server has been powered on. 2016-08-11T04:54:46.942Z Cold Start. 2016-08-11T04:54:22.031Z Connection deployed on downlink port 11, subport a. Connection enforcement succeeded. 2016-08-10T18:41:14.590Z Flex NIC subport state on Encl2, interconnect 1, port d11 changed. 2016-08-10T18:36:17.916Z Cold Start. 2016-08-10T18:36:17.866Z The server has been powered on. 2016-08-10T18:35:55.196Z The server has been powered off. 2016-08-10T18:31:00.164Z Flex NIC subport state on Encl2, interconnect 1, port d11 changed. 2016-08-10T18:30:46.656Z The server has been powered on. 2016-08-10T18:30:46.613Z Cold Start. 2016-08-10T18:30:27.092Z Connection deployed on downlink port 11, subport a. Connection enforcement succeeded. 2016-08-09T18:03:22.742Z The server has been powered off. 2016-08-09T18:01:30.071Z The server has been powered on. 2016-08-09T18:01:29.954Z Cold Start. 2016-08-09T04:01:46.719Z For uplink set 3PAR-uplink-A, all uplink ports are operational. 2016-08-09T04:01:46.620Z For uplink set PROD-uplink, all uplink ports are operational. 2016-08-09T04:01:46.525Z For uplink set DEV-uplink, all uplink ports are operational. 2016-08-09T04:01:43.456Z The stacking health has changed to redundantly connected. 2016-08-09T04:01:43.278Z For uplink set DEV-uplink, all uplink ports are operational. 2016-08-09T04:01:43.102Z For uplink set 3PAR-uplink-A, all uplink ports are operational. 2016-08-09T04:01:42.943Z For uplink set PROD-uplink, all uplink ports are operational. 2016-08-09T04:01:39.694Z The stacking health has changed to redundantly connected. 2016-08-09T04:01:37.810Z For uplink set 3PAR-uplink-A, uplink ports Encl2, interconnect 1 :X3 are non operational. 2016-08-09T04:01:37.710Z For uplink set PROD-uplink, uplink ports Encl2, interconnect 1 :X1 are non operational. 2016-08-09T04:01:37.582Z For uplink set DEV-uplink, uplink ports Encl2, interconnect 1 :X2 are non operational.