Skip to content

Commit

Permalink
Merge pull request #26 from EncoreTechnologies/feature/nic-actions
Browse files Browse the repository at this point in the history
feature/nic-actions
  • Loading branch information
bishopbm1 authored Aug 14, 2024
2 parents 1a2af21 + 8a07a88 commit 24ab646
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 0 deletions.
19 changes: 19 additions & 0 deletions actions/lib/action_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from st2common.runners.base_action import Action
from collections import OrderedDict
import requests
import time
import ssl
import pyone
import xmlrpc
Expand Down Expand Up @@ -110,6 +111,24 @@ def get_all_vm_ids(self, one):
vm_ids.sort()

return vm_ids

def wait_for_vm(self, one, vm_id, timeout=30):
# Wait for VM to enter LCM State of 3 = 'running'
lcm_state = one.vm.info(vm_id).LCM_STATE
start_time = time.time()
while lcm_state != 3:
# Set elapsed time for timeout check
elapsed_time = time.time() - start_time
if elapsed_time > timeout:
return lcm_state

# Wait before checking again
time.sleep(5)

# Update the image state for next check
lcm_state = one.vm.info(vm_id).LCM_STATE

return lcm_state

def run(self, **kwargs):
raise RuntimeError("run() not implemented")
39 changes: 39 additions & 0 deletions actions/vm_nic_attach.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env python
# Copyright 2024 Encore Technologies
#
# 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 "AS IS" 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 lib.action_base import BaseAction


class VmNicAttach(BaseAction):
def run(self, attach_timeout, gateway, ip_addr, method, network_id, vm_id, open_nebula=None):
# Create one session
one = self.pyone_session_create(open_nebula)

# Create nic attributes
nic_attributes = "NIC=[IP={}, NETWORK_ID={}, GATEWAY={}, METHOD={}]".format(ip_addr,
network_id,
gateway,
method)

# Attach nic
one.vm.attachnic(vm_id, nic_attributes)

# Wait for nic to attach
lcm_state = self.wait_for_vm(one, vm_id, attach_timeout)

# Check if nic was attached
if lcm_state != 3:
return (False, "Timed out waiting for VM {} to enter running state after attaching NIC".format(vm_id))
else:
return (True, "Succesfully attached NIC to VM {}".format(vm_id))
39 changes: 39 additions & 0 deletions actions/vm_nic_attach.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
name: vm_nic_attach
runner_type: python-script
description: Attaches a nic to the given VM
enabled: true
entry_point: vm_nic_attach.py
parameters:
attach_timeout:
type: integer
description: How long to wait to confirm the nic is attached
required: false
default: 30
gateway:
type: string
description: Gateway to specify for the NIC
required: true
ip_addr:
type: string
description: IP Address to specify for the NIC
required: true
method:
type: string
description: Method to specify for the NIC
required: false
default: "static"
network_id:
type: integer
description: ID of the Network in Open Nebula to use for the NIC
required: true
vm_id:
type: integer
description: ID of the VM in Open Nebula
required: true
open_nebula:
type: string
description: >
Pre-Configured Open Nebula connection details
required: false
default: ~
33 changes: 33 additions & 0 deletions actions/vm_nic_detach.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python
# Copyright 2024 Encore Technologies
#
# 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 "AS IS" 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 lib.action_base import BaseAction


class VmNicDetach(BaseAction):
def run(self, detach_timeout, nic_id, vm_id, open_nebula=None):
# Create one session
one = self.pyone_session_create(open_nebula)

# Detach nic
one.vm.detachnic(vm_id, nic_id)

# Wait for nic to detach
lcm_state = self.wait_for_vm(one, vm_id, detach_timeout)

# Check if nic was detached
if lcm_state != 3:
return (False, "Timed out waiting for VM {} to enter running state after detaching NIC".format(vm_id))
else:
return (True, "Succesfully detached NIC from VM {}".format(vm_id))
26 changes: 26 additions & 0 deletions actions/vm_nic_detach.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
name: vm_nic_detach
runner_type: python-script
description: Detaches a nic from the given VM
enabled: true
entry_point: vm_nic_detach.py
parameters:
detach_timeout:
type: integer
description: How long to wait to confirm the nic is detached
required: false
default: 30
nic_id:
type: integer
description: ID of the nic on the VM that we want to detach
required: true
vm_id:
type: integer
description: ID of the VM to detach the nic from
required: true
open_nebula:
type: string
description: >
Pre-Configured Open Nebula connection details
required: false
default: ~

0 comments on commit 24ab646

Please sign in to comment.