예제 #1
0
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')
예제 #2
0
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')
예제 #3
0
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')
예제 #4
0
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')
예제 #5
0
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)
예제 #6
0
파일: l23filter.py 프로젝트: mvallini/vnsfs
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)
예제 #7
0
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')
예제 #8
0
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')
예제 #9
0
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')
예제 #10
0
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})
예제 #11
0
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')
예제 #12
0
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')
예제 #13
0
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')
예제 #14
0
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')
예제 #15
0
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})
예제 #16
0
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')
예제 #17
0
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')
예제 #18
0
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')
예제 #19
0
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')
예제 #20
0
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')
예제 #21
0
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')
예제 #22
0
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')
예제 #23
0
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')
예제 #24
0
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')
예제 #25
0
파일: ids.py 프로젝트: shield-h2020/vnsfs
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)
예제 #26
0
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')
예제 #27
0
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')
예제 #28
0
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')
예제 #29
0
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')
예제 #30
0
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')
예제 #31
0
def restart_hss():
    cmd = "sudo systemctl restart nextepc-hssd"
    charms.sshproxy._run(cmd)
    remove_flag('actions.restart-hss')
예제 #32
0
def restart_spgw():
    cmd = "sudo systemctl restart nextepc-mmed"
    charms.sshproxy._run(cmd)
    remove_flag('actions.restart-spgw')