def start_transcoder(): try: stream_ip = action_get('stream-ip') output_port = action_get('output-port') cmd = "sudo rm /etc/systemd/system/opencv.service >/dev/null 2>&1; " cmd += "sudo systemctl stop opencv.service >/dev/null 2>&1; " cmd += "sudo systemctl daemon-reload" charms.sshproxy._run(cmd) cmd = "echo '[Unit]' | sudo tee -a /etc/systemd/system/opencv.service > /dev/null && " cmd += "echo 'Description=OpenCV' | sudo tee -a /etc/systemd/system/opencv.service > /dev/null && " cmd += "echo '' | sudo tee -a /etc/systemd/system/opencv.service > /dev/null && " cmd += "echo '[Service]' | sudo tee -a /etc/systemd/system/opencv.service > /dev/null && " cmd += "echo 'Type=simple' | sudo tee -a /etc/systemd/system/opencv.service > /dev/null && " cmd += "echo 'User=ubuntu' | sudo tee -a /etc/systemd/system/opencv.service > /dev/null && " cmd += "echo 'WorkingDirectory=/home/ubuntu' | sudo tee -a /etc/systemd/system/opencv.service > /dev/null && " cmd += "echo 'ExecStart=/usr/bin/python live_server.py {0} {1}' | sudo tee -a /etc/systemd/system/opencv.service > /dev/null && ".format( stream_ip, output_port) cmd += "echo 'Restart=always' | sudo tee -a /etc/systemd/system/opencv.service > /dev/null && " cmd += "echo 'RestartSec=5' | sudo tee -a /etc/systemd/system/opencv.service > /dev/null && " cmd += "echo '' | sudo tee -a /etc/systemd/system/opencv.service > /dev/null && " cmd += "echo '[Install]' | sudo tee -a /etc/systemd/system/opencv.service > /dev/null && " cmd += "echo 'WantedBy=multi-user.target' | sudo tee -a /etc/systemd/system/opencv.service > /dev/null && " cmd += "sudo systemctl daemon-reload && " cmd += "sudo systemctl start opencv.service" result, _ = charms.sshproxy._run(cmd) except: exc_type, exc_value, exc_traceback = sys.exc_info() err = traceback.format_exception(exc_type, exc_value, exc_traceback) action_fail('Starting transcoder failed: ' + str(err)) else: action_set({'output': result}) finally: remove_flag('actions.start-transcoder')
def start(): try: # Bring up the eth1 interface. # The selinux label on the file needs to be set correctly cmd = "sudo timeout 5 /sbin/restorecon -v /etc/sysconfig/network-scripts/ifcfg-eth1" result, err = charms.sshproxy._run(cmd) except Exception as e: err = "{}".format(e) action_fail('command failed: {}, errors: {}'.format(err, e.output)) remove_flag('actions.start') return # Attempt to raise the non-mgmt interface, but ignore failures if # the interface is already up. try: cmd = "sudo timeout 30 /sbin/ifup eth1" result, err = charms.sshproxy._run(cmd) except Exception as e: pass try: cmd = "sudo timeout 30 /usr/bin/systemctl start {}". \ format(cfg['mode']) result, err = charms.sshproxy._run(cmd) except Exception as e: action_fail('command failed: {}, errors: {}'.format(e, e.output)) else: action_set({'stdout': result, 'errors': err}) finally: remove_flag('actions.start')
def ansible_playbook(): try: # edit ansible hosts file with the VNF parameters h = open("/etc/ansible/hosts", "wt") h.write("[targets]\n") #h1 = "{} ansible_connection=ssh ansible_ssh_user={} ansible_ssh_pass={}\n".format(cfg['ssh-hostname'],cfg['ssh-username'],cfg['ssh-password']) h1 = "{} ansible_connection=ssh ansible_ssh_user={} ansible_ssh_pass={} ansible_become_pass={}\n".format( cfg['ssh-hostname'], cfg['ssh-username'], cfg['ssh-password'], cfg['ssh-password']) h.write(h1) h.close() # edit ansible config to enable ssh connection with th VNF c = open("/etc/ansible/ansible.cfg", "wt") c.write("[defaults]\n") c.write("host_key_checking = False\n") c.close() # execute the ansible playbook path = find('playbook.yaml', '/var/lib/juju/agents/') call = ['ansible-playbook', path] subprocess.check_call(call) status_set('active', 'ready!') except Exception as e: action_fail('command failed: {}'.format(e)) else: remove_flag('actions.ansible-playbook') finally: remove_flag('actions.ansible-playbook')
def ansible_playbook(): try: # Retrieve the ssh parameter cfg = config() # edit ansible hosts file with the VNF parameters h = open("/etc/ansible/hosts", "wt") h.write("[test]\n") h1 = "{} ansible_connection=ssh ansible_ssh_user={} ansible_ssh_pass={} ansible_python_interpreter=/usr/bin/python3\n".format( cfg['ssh-hostname'], cfg['ssh-username'], cfg['ssh-password']) h.write(h1) h.close() # edit ansible config to enable ssh connection with th VNF c = open("/etc/ansible/ansible.cfg", "wt") c.write("[defaults]\n") c.write("host_key_checking = False\n") c.close() # execute the ansible playbook path = find('playbook.yaml', '/var/lib/juju/agents/') call = ['ansible-playbook', path] subprocess.check_call(call) except Exception as e: action_fail('command failed: {}, errors: {}'.format(e, e.output)) remove_flag('actions.ansible-playbook') return finally: remove_flag('actions.ansible-playbook')
def curl_call(action_name, path, method, headers={}, data=""): #log("curl_call: start") try: import requests resp = None curl_url = "http://{}:{}{}".format(rest_api_hostname, rest_api_port, path) log("curl_call: URL is " + curl_url + " and method is " + method) request_method = getattr(requests, method.lower()) resp = request_method(curl_url, headers=headers, data=data, verify=False) result = resp.text log("curl_call: result " + result) except Exception as e: exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] action_fail("command failed: {}, endpoint: {}:{}, filename: {}, \ line: {}".format(e, rest_api_hostname, rest_api_port, fname, exc_tb.tb_lineno)) else: action_set({"stdout": result}) finally: remove_flag(action_name)
def ssh_call(action_name, cmd): try: result, err = charms.sshproxy._run(cmd) except Exception as e: action_fail("command failed: {}, errors: {}".format(e, e.output)) else: action_set({"stdout": result, "errors": err}) finally: remove_flag(action_name)
def reboot(): err = '' try: result, err = charms.sshproxy._run("reboot") except: action_fail('command failed:' + err) else: action_set({'outout': result}) finally: remove_flag('actions.reboot')
def traceroute(): try: result, err = _run('traceroute -m {} {}'.format(action_get('hops'), action_get('destination'))) except: action_fail('traceroute command failed') else: # Here you can send results back from ping, if you had time to parse it action_set({'output': result}) finally: remove_flag('actions.traceroute')
def nmap(): err = '' try: result, err = _run('nmap {}'.format(action_get('destination'))) except: action_fail('nmap command failed:' + err) else: action_set({'outout': result}) finally: remove_flag('actions.nmap')
def pingme_forreal(): try: result, err = run('ping -qc {} {}'.format(action_get('count'), action_get('destination'))) except: action_fail('ping command failed') finally: remove_flag('actions.ping') # Here you can send results back from ping, if you had time to parse it action_set({'output': result})
def hostname(): err = '' try: cmd = "hostname" result, err = charms.sshproxy._run(cmd) except: action_fail('command failed:' + err) else: action_set({'outout': result}) finally: remove_flag('actions.set-server')
def stop_traffic(): try: cmd = format_curl('POST', '/adminstatus/state', '{"enable" : false}') result, err = charms.sshproxy._run(cmd) except Exception as e: action_fail('command failed: {}, errors: {}'.format(e, e.output)) else: action_set({'stdout': result, 'errors': err}) finally: remove_flag('actions.stop-traffic')
def get_stats(): try: cmd = format_curl('GET', '/stats') result, err = charms.sshproxy._run(cmd) except Exception as e: action_fail('command failed: {}, errors: {}'.format(e, e.output)) else: action_set({'stdout': result, 'errors': err}) finally: remove_flag('actions.get-stats')
def stop(): try: # Enter the command to stop your service(s) cmd = "sudo timeout 30 /usr/bin/systemctl stop {}".format(cfg['mode']) result, err = charms.sshproxy._run(cmd) except Exception as e: action_fail('command failed: {}, errors: {}'.format(e, e.output)) else: action_set({'stdout': result, 'errors': err}) finally: remove_flag('actions.stop')
def start_configuration(): try: # Enter the command to restart your service(s) # This cmd run the user configuration script cmd = "source iptables.sh" result, err = charms.sshproxy._run(cmd) except Exception as e: action_fail('command failed: {}, errors: {}'.format(e, e.output)) else: action_set({'stdout': result, 'errors': err}) finally: remove_flag('actions.start-configuration')
def touch(): err = '' try: filename = action_get('filename') cmd = ['touch {}'.format(filename)] result, err = charms.sshproxy._run(cmd) except: action_fail('command failed:' + err) else: action_set({'outout': result}) finally: remove_flag('actions.touch')
def configure_hss(): spgw_ip = action_get('spgw-ip') hss_ip = action_get('hss-ip') cmd1 = "sudo ip link set ens4 up && sudo dhclient ens4" charms.sshproxy._run(cmd1) cmd2 = 'sudo sed -i "\'s/$hss_ip/{}/g\'" /etc/nextepc/freeDiameter/hss.conf'.format( hss_ip) charms.sshproxy._run(cmd2) cmd3 = 'sudo sed -i "\'s/$spgw_ip/{}/g\'" /etc/nextepc/freeDiameter/hss.conf'.format( spgw_ip) charms.sshproxy._run(cmd3) remove_flag('actions.configure-hss')
def pass_files(): try: # Enter the command to stop your service(s) cmd = "sudo chmod 777 -R ./" result, err = charms.sshproxy._run(cmd) cmd = "scp -i chiavefile.pem -o \"StrictHostKeyChecking no\" [email protected]:/Users/ignaziopedone/configurazioni/iptables.sh /home/ubuntu" result, err = charms.sshproxy._run(cmd) except Exception as e: action_fail('command failed: {}, errors: {}'.format(e, e.output)) else: action_set({'stdout': result, 'errors': err}) finally: remove_flag('actions.pass-files')
def start(): try: # Enter the command to restart your service(s) # This allows to configure the newtwork interfaces and the SFC cmd = "source network.sh" result, err = charms.sshproxy._run(cmd) cmd = "source script.sh" result, err = charms.sshproxy._run(cmd) except Exception as e: action_fail('command failed: {}, errors: {}'.format(e, e.output)) else: action_set({'stdout': result, 'errors': err}) finally: remove_flag('actions.start')
def ping(): err = '' try: result, err = charms.sshproxy._run('ping -qc {} {}'.format( action_get('count'), action_get('destination')) ) except: action_fail('ping command failed:' + err) else: # Here you can send results back from ping, if you had time to parse it action_set({'output': result}) finally: remove_flag('actions.ping')
def set_rate(): try: if is_ping(): rate = action_get('rate') cmd = format_curl('POST', '/rate', '{{"rate" : {}}}'.format(rate)) result, err = charms.sshproxy._run(cmd) except Exception as e: err = "{}".format(e) action_fail('command failed: {}, errors: {}'.format(err, e.output)) else: action_set({'stdout': result, 'errors': err}) finally: remove_flag('actions.set-rate')
def iperf3(): err = '' try: # TODO: read all the flags via action_get and build the # proper command line to run iperf3 host = action_get('host') cmd = 'iperf3 -c {} --json'.format(host) result, err = charms.sshproxy._run(cmd) except CalledProcessError as e: action_fail('iperf3 command failed:' + e.output) else: action_set({'outout': result}) finally: remove_flag('actions.iperf3')
def configure_spgw(): hss_ip = action_get('hss-ip') spgw_ip = action_get('spgw-ip') cmd1 = "sudo ip link set ens4 up && sudo dhclient ens4" charms.sshproxy._run(cmd1) cmd2 = "sudo ip link set ens5 up && sudo dhclient ens5" charms.sshproxy._run(cmd2) cmd3 = "sudo ip link set ens6 up && sudo dhclient ens6" charms.sshproxy._run(cmd3) cmd3 = 'sudo sed -i "\'s/$hss_ip/{}/g\'" /etc/nextepc/freeDiameter/mme.conf'.format( hss_ip) charms.sshproxy._run(cmd3) cmd4 = 'sudo sed -i "\'s/$spgw_ip/{}/g\'" /etc/nextepc/freeDiameter/mme.conf'.format( spgw_ip) charms.sshproxy._run(cmd4) remove_flag('actions.configure-spgw')
def curl_call(action_name, path, method, headers={}, data=""): try: import requests resp = None curl_url = "http://{}:{}{}".format(rest_ip, rest_port, path) request_method = getattr(requests, method.lower()) resp = request_method(curl_url, headers=headers, data=data, verify=False) result = resp.text except Exception as e: exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] action_fail("command failed: {}, endpoint: {}:{}, filename: {}, line: {}".format(e, rest_ip, rest_port, fname, exc_tb.tb_lineno)) else: action_set({"stdout": result, "errors": e}) finally: remove_flag(action_name)
def set_server(): try: # Get the target service info target_ip = action_get('server-ip') target_port = action_get('server-port') data = '{{"ip" : "{}", "port" : {} }}'. \ format(target_ip, target_port) cmd = format_curl( 'POST', '/server', data, ) result, err = charms.sshproxy._run(cmd) except Exception as e: action_fail('command failed: {}, errors: {}'.format(e, e.output)) else: action_set({'stdout': result, 'errors': err}) finally: remove_flag('actions.set-server')
def dig(): err = '' try: nsserver = action_get('nsserver') host = action_get('host') nstype = action_get('type') cmd = "dig" if nsserver: cmd += " @{}".format(nsserver) if host: cmd += " {}".format(host) else: action_fail('Hostname required.') if nstype: cmd += " -t {}".format(nstype) result, err = charms.sshproxy._run(cmd) except: action_fail('dig command failed:' + err) else: action_set({'outout': result}) finally: remove_flag('actions.dig')
def test_connection(): status_set('maintenance', 'configuring ssh connection') remove_flag('vyos-proxy.ready') try: who, _ = run('whoami') except MgmtNotConfigured as e: remove_flag('vyos-proxy.configured') status_set('blocked', str(e)) except subprocess.CalledProcessError as e: remove_flag('vyos-proxy.configured') status_set('blocked', e.output) else: set_flag('vyos-proxy.configured')
def add_route(): prefix = action_get('external-prefix') next_hop = action_get('next-hop') cmd = "sudo route add -net " + prefix + " gw " + next_hop charms.sshproxy._run(cmd) remove_flag('actions.add-route')
def restart_hss(): cmd = "sudo systemctl restart nextepc-hssd" charms.sshproxy._run(cmd) remove_flag('actions.restart-hss')
def restart_spgw(): cmd = "sudo systemctl restart nextepc-mmed" charms.sshproxy._run(cmd) remove_flag('actions.restart-spgw')