Esempio n. 1
0
class test_AwsClientUtils(unittest.TestCase):
    def setUp(self):
        self.awsClientUtils = AwsClientUtils()

    def testregion_exists_true(self):
        regionUsEast1 = "us-east-1"
        self.assertTrue(self.awsClientUtils.region_exists(regionUsEast1))
        regionSaEast1 = "sa-east-1"
        self.assertTrue(self.awsClientUtils.region_exists(regionSaEast1))

    def testregion_exists_false(self):
        regionNoWhere = "nw-east-1"
        self.assertFalse(self.awsClientUtils.region_exists(regionNoWhere))
Esempio n. 2
0
def main():

    parser = argparse.ArgumentParser()

    parser = mass_parser_arguments([
        ["access", "a", False, "Set a way to access the instance if you are creating a new one"],
        ["command", "c", False, "Defines a specific action"],
        ["distro", "d", False, "A name of a distribuition, if required a specific one"],
        ["filter-name", "fn", False, "Search for instance with the tag name"],
        ["filter-status", "fs", False, "A status filter if desired"],
        ["id", "i", False, "The instance id required for some commands"],
        ["lasts", "l", False, "Say how much time the instance will lasts to avoid unexpected costs"],
        ["name", "n", False, "Set the names's tag"],
        ["profile", "p", False, "Set the aws cli profile, if needed"],
        ["region", "r", False, "Restrict search just for a single region"],
        ["status-filter", "sf", False, "Filter instance by status"],
        ["user-data", "u", False, "Path for user data as shell script for instance"],
    ], parser)

    args = parser.parse_args()

    if args.profile:
        profile = args.profile
    else:
        profile = AWSUtils().guessLocalProfile()

    if profile == "":
        print("I cound not guess credentials, sorry. Explicitly set a profile name using -p or --profile or set in the aws configuration using the command: \"aws configure --profile <your_profile>\".")
        exit()

    if args.region != None and not AwsClientUtils().region_exists(args.region):
        print("The given region does not exists. Exiting.")
        exit()

    commands = Commands(profile, args.region)

    if not args.command or args.command == "list":
        commands.list(args.region, args.filter_status, args.filter_name)
    elif args.command == "new":
        create_new_instance(args, commands)
    elif args.command == "sleep":
        commands.sleep(args.id)
    elif args.command == "kill":
        commands.kill(args.id)
    elif args.command == "restart":
        commands.restart(args.id_to_restart)
    else:
        print("The command " + args.command + " does not exists.")
Esempio n. 3
0
    def new(
        self,
        protocolService: ProtocolService,
        user_script: str,
        vpc,
        distro=None,
    ):
        keypairname = None
        if protocolService.is_have_ssh():
            keypairname = AWSUtils().get_key_pair_name()
            if not keypairname:
                raise Exception(
                    'No keypair found to assign. You need it to access through ssh.'
                )

        region = self.aws_client.meta.region_name
        aws_resource = boto3.resource('ec2', region_name=region)

        subnet = VPC_Client().get_first_subnet(vpc)

        return AwsClientUtils().create_new_instance_resource(
            aws_resource, region, keypairname, user_script, subnet, distro)
Esempio n. 4
0
 def sleep(self, id_to_sleep):
     aws_resource = boto3.client(
         'ec2', region_name=self.aws_client.meta.region_name)
     for id in id_to_sleep.split(","):
         AwsClientUtils().sleep_instance(aws_resource, id)
Esempio n. 5
0
 def restart(self, id_to_restart):
     aws_resource = boto3.resource(
         'ec2', region_name=self.aws_client.meta.region_name)
     AwsClientUtils().restart_instance(aws_resource, id_to_restart)
Esempio n. 6
0
    def kill(self, id_to_kill):
        aws_resource = boto3.resource(
            'ec2', region_name=self.aws_client.meta.region_name)

        for id in id_to_kill.split(","):
            AwsClientUtils().kill_instance(aws_resource, id)
Esempio n. 7
0
 def setUp(self):
     self.awsClientUtils = AwsClientUtils()