Exemple #1
0
    def sync_formula(self, formula_name):
        salt_home_dir = '/srv/salt/'
        formula_dir = salt_home_dir + formula_name

        localstore = RxLocalStore.get_localstore()
        localstore.set_context(localstore.get_context()
                               + SaltSettings.FORMULAS_DIR
                               + '/'
                               + formula_name)

        client = SSHWrapper.with_connection_details(self.ssh_connectiondetails)
        client.send_blocking_command('sudo mkdir -p ' + salt_home_dir)
        client.send_blocking_command('sudo mkdir -p ' + formula_dir)

        zf = RxLocalCache.create_temp_archive(formula_name)
        for root, dirs, files in os.walk(localstore.get_filestore_location_with_context()):
            for file in files:
                zip_root = os.path.relpath(os.path.join(root, file),
                                           localstore.get_filestore_location_with_context())
                zf.write(os.path.join(root, file), zip_root)

        zf.close()
        client.send_file(zf.filename, formula_dir,sudo=True)
        client.send_blocking_command(
            'cd ' + formula_dir + ' && sudo unzip -o ' + ntpath.basename(zf.filename))
        client.send_blocking_command('cd ' + formula_dir + '&& sudo rm ' + ntpath.basename(zf.filename))
Exemple #2
0
formatter = logging.Formatter(
    '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)

localuser = LocalSettings.localuser
remoteuser = RemoteSettings.remoteuser
# retrieve the authentication token
token_result = REST_authentication().postCredentials(ApiUserSettings.username,
                                                     ApiUserSettings.password)
auth_token = token_result['token']
inputmapping = InputMapper().getInputFromCLI(auth_token)

data = json.loads(inputmapping.getKeyvalList())

client = SSHWrapper.with_keys(data['remoteuser'], inputmapping.getIpAddress(),
                              data['sshport'])

if data['os'] == "CentOS":
    # first remove salt, if it was already installed
    client.send_blocking_command('sudo yum install -y wget')
    client.send_blocking_command('sudo yum install -y unzip')
    client.send_blocking_command(
        'wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm'
    )
    client.send_blocking_command(
        'sudo rpm -ivh epel-release-latest-7.noarch.rpm')

    reststates_api = REST_states(auth_token)
    state = reststates_api.getStateByHostAndStateId(
        inputmapping.getGetHostId(), inputmapping.getStateId())
    state = state[0]
Exemple #3
0
logger.setLevel(logging.DEBUG)

ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter(
    '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)

token_result = REST_authentication().postCredentials(ApiUserSettings.username,
                                                     ApiUserSettings.password)
auth_token = token_result['token']

inputmapping = InputMapper().getInputFromCLI(auth_token)
data = json.loads(inputmapping.getKeyvalList())
resthosts_api = REST_hosts(auth_token)
host = resthosts_api.get_host_by_id(inputmapping.host_id)
hostname = host['hostname']
client = SSHWrapper.with_password(inputmapping.getIpAddress(),
                                  data["username"], data["password"],
                                  data['sshport'])
client.send_blocking_command("hostname " + hostname)
client.send_blocking_command("echo '" + hostname + "' > /etc/hostname")

reststates_api = REST_states(auth_token)
state = reststates_api.getStateByHostAndStateId(inputmapping.getGetHostId(),
                                                inputmapping.getStateId())
state = state[0]
statemanager = StateManager(auth_token)
statemanager.setSimpleStateInstalled(state)
    if not os.path.exists(tmp_dir):
        try:
            os.makedirs(tmp_dir, 0o700)
        except OSError as e:
            if e.errno != errno.EEXIST:
                raise

    id_rsa_pub = tmp_dir + 'id_rsa.pub'
    if os.path.exists(id_rsa):
        sh.rm(id_rsa)

    sh.ssh_keygen("-t", "rsa", "-f", id_rsa, _in="\n")
    rx_localstore = RxLocalStore()
    connection_details = ConnectionDetails(host_username, host_password,
                                           salt_master, False, ssh_port)
    ssh_login = SSHWrapper.with_connection_details(connection_details)
    ssh_login.send_blocking_command('mkdir /root/.ssh')
    ssh_login.send_file(id_rsa_pub, '/root/.ssh/authorized_keys')
    ssh_connection_details = ConnectionDetails.\
        new_connection_with_custom_key(host_username, host_password, salt_master, id_rsa, ssh_port)
elif salt_mapping.api_mode == 'PRODUCTION':
    id_rsa = LocalSettings.localconfig + '/id_rsa'
    ssh_connection_details = ConnectionDetails.new_connection_with_custom_key(
        remote_user, '', salt_master, id_rsa, ssh_port)

if salt_mapping.api_mode == 'SALTTESTVIRT' or salt_mapping.api_mode == 'SALTTESTDOCKER' or salt_mapping.api_mode == 'PRODUCTION':

    salt_connection_details = SaltConnectionDetails(salt_username,
                                                    salt_password, salt_master,
                                                    salt_api_port)
    salt_service = SaltService(ssh_connection_details, salt_connection_details,