Esempio n. 1
0
 def setUp(self):
     self.env_name = "test"
     self.stage = "prod"
     self.env_id = "12343434"
     self.build = '{"commit":"c5a7f50453fa70fefa41dc5b75e9b053fc5bba4b","id":"S2dUHIrFSMyDzdwO-6mgeA_c81d6b3","branch":"master","artifactUrl":"https://deployrepo.pinadmin.com/pinboard/pinboard-c5a7f50.tar.gz","repo":"P","name": "pinboard"}' 
     self.script_variables = '{"IS_DOCKER": "True"}'
     self.client = ServerlessClient(env_name=self.env_name, stage=self.stage, build=self.build,
                                    script_variables=self.script_variables) 
Esempio n. 2
0
def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('-e', '--server_stage', dest='stage', default='prod',
                        help="This option is deprecated")
    parser.add_argument('-f', '--config-file', dest='config_file', required=False,
                        help="the deploy agent config file path.")
    parser.add_argument('-d', '--daemon', dest="daemon", action='store_true',
                        help="Run deploy agent in daemon mode. Default is false.")
    parser.add_argument('-n', '--host', dest="hostname", required=False, default=None,
                        help="Host name being used when interact with Teletraan service. "
                             "This is optional. By default the hostname defined in host-info "
                             "file will be used")
    parser.add_argument('-g', '--group', dest='hostgroup', required=False, default=None,
                        help="Group name being used when interact with Teletraan service. "
                             "This is optional. By default the group name defined in host-info "
                             "file will be used")
    parser.add_argument('--use-facter', dest='use_facter', action='store_true', default=False)
    parser.add_argument('--mode', dest='mode', default=None,
                        help="Optional. 'serverless' is the only non default mode supported. "
                             "In this mode, agent can be run for one time deployment without "
                             "interacting with teletraan service.")
    parser.add_argument('--build', dest='build', default=None,
                        help="Optional. In 'serverless' mode, build information is needed in "
                             "json format.")
    parser.add_argument('--env-name', dest='env_name', default=None,
                        help="Optional. In 'serverless' mode, env_name needs to be passed in.")
    parser.add_argument('--script-variables', dest='script_variables', default='{}',
                        help="Optional. In 'serverless' mode,  script_variables is needed in "
                             "json format.")

    args = parser.parse_args()

    is_serverless_mode = AgentRunMode.is_serverless(args.mode)
    if args.daemon and is_serverless_mode:
        raise ValueError("daemon and serverless mode is mutually exclusive.")
    config = Config(args.config_file)
    utils.run_prereqs(config)

    if IS_PINTEREST:
        import pinlogger

        pinlogger.initialize_logger(logger_filename='deploy-agent.log')
        pinlogger.LOG_TO_STDERR = True
    else:
        log_filename = os.path.join(config.get_log_directory(), 'deploy-agent.log')
        logging.basicConfig(filename=log_filename, level=config.get_log_level(),
                            format='%(asctime)s %(name)s:%(lineno)d %(levelname)s %(message)s')

    log.info("Start to run deploy-agent.")
    client = Client(config=config, hostname=args.hostname, hostgroup=args.hostgroup,
                    use_facter=args.use_facter)
    if is_serverless_mode:
        log.info("Running agent with severless client")
        client = ServerlessClient(env_name=args.env_name, stage=args.stage, build=args.build,
                                  script_variables=args.script_variables)

    agent = DeployAgent(client=client, conf=config)
    utils.listen()
    if args.daemon:
        logger = logging.getLogger()
        handles = []
        for handler in logger.handlers:
            handles.append(handler.stream.fileno())
        with daemon.DaemonContext(files_preserve=handles):
            agent.serve_forever()
    else:
        agent.serve_once()
Esempio n. 3
0
class TestServerlessClient(tests.TestCase):

    def setUp(self):
        self.env_name = "test"
        self.stage = "prod"
        self.env_id = "12343434"
        self.build = '{"commit":"c5a7f50453fa70fefa41dc5b75e9b053fc5bba4b","id":"S2dUHIrFSMyDzdwO-6mgeA_c81d6b3","branch":"master","artifactUrl":"https://deployrepo.pinadmin.com/pinboard/pinboard-c5a7f50.tar.gz","repo":"P","name": "pinboard"}' 
        self.script_variables = '{"IS_DOCKER": "True"}'
        self.client = ServerlessClient(env_name=self.env_name, stage=self.stage, build=self.build,
                                       script_variables=self.script_variables) 

    def _new_report(self):
        report = PingReport()
        report.envName = self.env_name
        report.stageName = self.stage
        report.erroCode = 0
        report.envId  = self.env_id
        report.deployStage = None
        report.status = AgentStatus.SUCCEEDED
        return report
              
    def test_deploy_stage_trnasition(self):
        report = self._new_report()
        deploy_status = DeployStatus()
        deploy_status.report = report
        env_status = {self.env_name : deploy_status}

        deployStages = ['PRE_DOWNLOAD', 'DOWNLOADING', 'POST_DOWNLOAD', 'STAGING', 'PRE_RESTART', 'RESTARTING', 'POST_RESTART', 'SERVING_BUILD']

        for i in range(0, len(deployStages)):
            response = self.client.send_reports(env_status) 
            self.assertEqual(response.opCode, "DEPLOY")
            self.assertEqual(response.deployGoal.deployStage, deployStages[i])
            report.deployStage = response.deployGoal.deployStage
            report.deployId = response.deployGoal.deployId

        # test ending case
        response = self.client.send_reports(env_status)
        self.assertEqual(response.deployGoal, None)
 
    def test_errorcode_stop_deployment(self):
        report = self._new_report()
        deploy_status = DeployStatus()
        deploy_status.report = report
        env_status = {self.env_name : deploy_status}

        # first try is allowed.
        report.errorCode = 123 
        response = self.client.send_reports(env_status)
        report.deployStage = response.deployGoal.deployStage
        report.deployId = response.deployGoal.deployId

        response = self.client.send_reports(env_status)
        self.assertEqual(response, None)

    def test_unknow_status_cause_retry(self):
        report = self._new_report()
        deploy_status = DeployStatus()
        deploy_status.report = report
        env_status = {self.env_name : deploy_status}

        report.status = AgentStatus.UNKNOWN 
        response = self.client.send_reports(env_status)
        report.deployStage = response.deployGoal.deployStage
        report.deployId = response.deployGoal.deployId

        response = self.client.send_reports(env_status)
        self.assertEqual(response.deployGoal.deployStage, 'PRE_DOWNLOAD')