예제 #1
0
class RemoteClientTestCase(unittest.TestCase):
    def setUp(self):
        with patch("clustermgr.core.remote.SSHClient") as mock_client:
            self.sshclient = mock_client.return_value
            # self.sshclient.open_sftp.return_value = MagicMock(name="sftp")
            self.rc = RemoteClient('server')
            self.rc.startup()

    @patch('clustermgr.core.remote.SSHClient')
    def test_starup_falls_back_to_ip(self, mock_client):
        instance = mock_client.return_value
        instance.connect = MagicMock(side_effect=[SSHException, None])
        RemoteClient('server', ip='0.0.0.0').startup()
        instance.connect.assert_called_with('0.0.0.0',
                                            port=22,
                                            username='******')

    def test_download_calls_sftpclient_get(self):
        rv = self.rc.download('remote', 'local')
        self.rc.sftpclient.get.assert_called_with('remote', 'local')
        assert "successful" in rv

    def test_download_returns_errors_when_get_throws_errors(self):
        self.rc.sftpclient.get.side_effect = OSError
        rv = self.rc.download('remote', 'local_1')
        self.assertIn('Error: Local', rv)

        self.rc.sftpclient.get.side_effect = IOError
        rv = self.rc.download('remote', 'local_2')
        self.assertIn('Error: Remote', rv)

    def test_upload_calls_sftpclient_put(self):
        rv = self.rc.upload('local', 'remote')
        self.rc.sftpclient.put.assert_called_with('local', 'remote')
        assert "successful" in rv

    def test_upload_returns_errors_when_put_throws_errors(self):
        self.rc.sftpclient.put.side_effect = IOError
        rv = self.rc.upload('local', 'remote')
        self.assertIn('Error: Remote', rv)

        self.rc.sftpclient.put.side_effect = OSError
        rv = self.rc.upload('local_2', 'remote_2')
        self.assertIn('Error: Local', rv)

    def test_upload_and_download_throws_error_if_sftpclient_is_none(self):
        self.rc.sftpclient = None
        with self.assertRaises(ClientNotSetupException):
            self.rc.upload('s', 't')
        with self.assertRaises(ClientNotSetupException):
            self.rc.download('s', 't')

    def test_exists_and_run_throws_error_if_client_is_none(self):
        self.rc.client = None
        with self.assertRaises(ClientNotSetupException):
            self.rc.exists('s')
        with self.assertRaises(ClientNotSetupException):
            self.rc.run('s')
예제 #2
0
def wizard_step1(self):
    """Celery task that collects information about server.

    :param self: the celery task

    :return: the number of servers where both stunnel and redis were installed
        successfully
    """

    tid = self.request.id

    wlogger.log(tid, "Analayzing Current Server")

    server = Server.query.filter_by(primary_server=True).first()

    app_conf = AppConfiguration.query.first()

    c = RemoteClient(server.hostname, ip=server.ip)

    try:
        c.startup()
        wlogger.log(tid, "SSH connection established", 'success')
    except:
        wlogger.log(tid, "Can't establish SSH connection", 'fail')
        wlogger.log(tid, "Ending analyzation of server.", 'error')
        return

    os_type = get_os_type(c)

    server.os = os_type

    wlogger.log(tid, "OS type was determined as {}".format(os_type), 'debug')

    gluu_version = None

    #Determine if a version of gluu server was installed.
    r = c.listdir("/opt")
    if r[0]:
        for s in r[1]:
            m = re.search(
                'gluu-server-(?P<gluu_version>(\d+).(\d+).(\d+)(.\d+)?)$', s)
            if m:
                gluu_version = m.group("gluu_version")

                app_conf.gluu_version = gluu_version
                wlogger.log(
                    tid,
                    "Gluu version was determined as {}".format(gluu_version),
                    'debug')

    if not gluu_version:
        wlogger.log(tid, "Gluu server was not installed on this server",
                    'fail')
        wlogger.log(tid, "Ending analyzation of server.", 'error')
        return

    gluu_path = '/opt/gluu-server-{}'.format(gluu_version)

    server.gluu_server = True

    setup_properties_last = os.path.join(
        gluu_path, 'install/community-edition-setup/setup.properties.last')

    setup_properties_local = os.path.join(Config.DATA_DIR, 'setup.properties')

    result = c.download(setup_properties_last, setup_properties_local)

    prop = get_setup_properties()
    prop['hostname'] = app_conf.nginx_host
    write_setup_properties_file(prop)

    if not result.startswith('Download successful'):
        wlogger.log(tid, result, 'fail')
        wlogger.log(tid, "Ending analyzation of server.", 'error')
        return

    wlogger.log(tid, "setup.properties file was downloaded", 'debug')

    server.ldap_password = prop['ldapPass']

    wlogger.log(tid, "LDAP Bind password was identifed", 'success')

    db.session.commit()
예제 #3
0
def wizard_step1(self):
    
    """Celery task that collects information about server.

    :param self: the celery task

    :return: the number of servers where both stunnel and redis were installed
        successfully
    """
    
    tid = self.request.id

    wlogger.log(tid, "Analayzing Current Server")

    server = Server.query.filter_by(primary_server=True).first()

    app_conf = AppConfiguration.query.first()
    
    c = RemoteClient(server.hostname, ip=server.ip)

    try:
        c.startup()
        wlogger.log(tid, "SSH connection established", 'success')
    except:
        wlogger.log(tid, "Can't establish SSH connection",'fail')
        wlogger.log(tid, "Ending analyzation of server.", 'error')
        return

    os_type = get_os_type(c)
    
    server.os = os_type
    
    wlogger.log(tid, "OS type was determined as {}".format(os_type), 'debug')
    
    gluu_path_version = None
    oxauth_version = None

    #Determine if a version of gluu server was installed.
    r = c.listdir("/opt")
    if r[0]:
        for s in r[1]:
            m=re.search('gluu-server-(?P<gluu_version>(\d+).(\d+).(\d+)(.\d+)?)$',s)
            if m:
                gluu_path_version = m.group("gluu_version")
                
                wlogger.log(tid, "Gluu path was determined as gluu-server-{}".format(
                                                        gluu_path_version), 'debug')
                
                
                oxauth_path = '/opt/gluu-server-{0}/opt/gluu/jetty/oxauth/webapps/oxauth.war'.format(gluu_path_version)
                
                try:
                    result = c.run('''python -c "import zipfile;zf=zipfile.ZipFile('{}','r');print zf.read('META-INF/MANIFEST.MF')"'''.format(oxauth_path))
                
                    menifest = result[1]

                    for l in menifest.split('\n'):
                        ls = l.strip()
                        if 'Implementation-Version:' in ls:
                            version = ls.split(':')[1].strip()
                            oxauth_version = '.'.join(version.split('.')[:3])
                            
                            wlogger.log(tid, "oxauth version was determined as {}".format(
                                                                oxauth_version), 'debug')
                            app_conf.gluu_version = oxauth_version
                            break
                except:
                    pass

                if not oxauth_version:
                    wlogger.log(tid, "Error determining oxauth version.", 'debug')
                    wlogger.log(tid, "Setting gluu version to path version", 'debug')            
                    app_conf.gluu_version = gluu_path_version

    
    if not gluu_path_version:
        wlogger.log(tid, "Gluu server was not installed on this server",'fail')
        wlogger.log(tid, "Ending analyzation of server.", 'error')
        return
    

    gluu_path = '/opt/gluu-server-{}'.format(gluu_path_version)
    
    server.gluu_server = True
    
    setup_properties_last = os.path.join(gluu_path, 
                        'install/community-edition-setup/setup.properties.last')
    
    setup_properties_local = os.path.join(Config.DATA_DIR, 'setup.properties')
    
    result = c.download(setup_properties_last, setup_properties_local)
    
    prop = get_setup_properties()
    prop['hostname'] = app_conf.nginx_host
    write_setup_properties_file(prop)
    
    
    if not result.startswith('Download successful'):
        wlogger.log(tid, result,'fail')
        wlogger.log(tid, "Ending analyzation of server.", 'error')
        return
    
    wlogger.log(tid, "setup.properties file was downloaded", 'debug')
    
    server.ldap_password = prop['ldapPass']
    
    wlogger.log(tid, "LDAP Bind password was identifed", 'success')
    
    db.session.commit()