diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..fe71598 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.linting.pylintEnabled": false +} \ No newline at end of file diff --git a/lab-01/file_ops.py b/lab-01/file_ops.py index 37c371c..0611731 100644 --- a/lab-01/file_ops.py +++ b/lab-01/file_ops.py @@ -1,18 +1,37 @@ #!/usr/bin/python - # Open the license file, read the content into the buffer # then close the file descriptor -f = open('conditions.py') -lines10 = f.read(10) -#alllines = f.read() -print (lines10) -f.close() +# f = open('conditions.py') +# lines10 = f.read(10) +# # #alllines = f.read() +# print (lines10) +# f.close() # Open the license file, read the content as "lines" into the # buffer and close it. f = open('conditions.py') file_content = f.readlines() -f.close() \ No newline at end of file +print file_content +f.close() + +for line in file_content: + #print line.split() + if len(line) !=0: + line_list = line.split() + if len(line_list) !=0: + if line_list[0] == 'print': + print line_list + +# READ 15-20 +# f = open('conditions.py') +# f.seek(15) +# line15 = f.read(5) + +# TRYING READ VS READLINES +# f = open('conditions.py') +# file_content = f.read() +# print file_content +# f.close() \ No newline at end of file diff --git a/lab-01/list-operations.py b/lab-01/list-operations.py index 2b52b84..ea04771 100644 --- a/lab-01/list-operations.py +++ b/lab-01/list-operations.py @@ -5,12 +5,23 @@ new_list = [] -for l in mylist: - #print l * 100 - new_list.append(l + 100) +for list_item in mylist: + new_item = list_item * 100 + #add_more = new_item +50 + new_list.append(new_item) + #new_list.append(add_more) + # print new_list + # print max(new_list) + # print min(new_list) + # print sum(new_list) -print new_list -print max(new_list) -print min(new_list) -print sum(new_list) +# for l in mylist: +# #print l * 100 +# new_list.append(l + 100) +# 2-10, put in a list +this_list = [] +for item in (range (2,10)): + #add10 = item + 10 + this_list.append(item) +print this_list \ No newline at end of file diff --git a/lab-02/Ex0910b.py b/lab-02/Ex0910b.py new file mode 100644 index 0000000..4ddbaa4 --- /dev/null +++ b/lab-02/Ex0910b.py @@ -0,0 +1,17 @@ +import multiprocessing + +import os + +def myfunction(): + print multiprocessing.cpu_count() + #print os.cpu_count +#kernel name + print os.uname()[0] +# load average + print os.getloadavg +#uptime + print os.system("uptime") + #disk usage + print os.system("df -h") + +print myfunction() \ No newline at end of file diff --git a/lab-02/exercise0910.py b/lab-02/exercise0910.py new file mode 100644 index 0000000..9b21102 --- /dev/null +++ b/lab-02/exercise0910.py @@ -0,0 +1,18 @@ +def myfunction(filename, num_lines): + #return error if the filename is less than 4 - length + if len(filename) > 4: +#copies arg2 number of lines of arg1 into home dir + f = open(filename) + f2 = f.read(filename) + home_dir_file = [] + home_dir_file = f2.split("\n") + print home_dir_file + #to slice to number of line - use colon + #n = int(num_lines) + print_this = home_dir_file[:num_lines] + for each in print_this: + print each + +# ~/devopstraining/ansible/ +myfunction("/Users/localadmin/devopstraining/ansible/mylogfile.log", 3) +print "error" diff --git a/lab-02/func-03.py b/lab-02/func-03.py new file mode 100644 index 0000000..0c53430 --- /dev/null +++ b/lab-02/func-03.py @@ -0,0 +1,7 @@ +def myfunction(arg1, arg2): +#multiply + answer = arg1 * arg2 +#return arg1 * arg2 +#print + print answer +myfunction(1,2) \ No newline at end of file diff --git a/lab-02/functions-args.py b/lab-02/functions-args.py index 497c21b..5108a21 100644 --- a/lab-02/functions-args.py +++ b/lab-02/functions-args.py @@ -2,6 +2,9 @@ def myfunction(arg1, arg2,arg3): print "My Very first python function with Arguments" print "{},{},{}".format(arg1, arg2, arg3) + sum = arg1 + arg2 + arg3 + + #return sum if arg3 == "chicago": second_func(arg1,arg2) @@ -21,4 +24,6 @@ def third_func(arg1): if __name__ == "__main__": - myfunction("hello","world","chicago") \ No newline at end of file + # myfunction(1,2,3) + myreturn = myfunction(1,2,3) + print my_return diff --git a/lab-06/E0910b.py b/lab-06/E0910b.py new file mode 100644 index 0000000..97854a4 --- /dev/null +++ b/lab-06/E0910b.py @@ -0,0 +1,22 @@ +import requests + +def count_yelp(text): + all_words = text.split() + yelp_count = all_words.count('Yelp') + return yelp_count + +def replace_yelp(text): + return text.replace('yelp','google') + +r = requests.get('http://wwww.yelp.com') +if r.status_code == 200: + # print r.content + print count_yelp(r.text) + #print replace_yelp(r.text) + +# Assignment + +# Load the yelp page and replace all occurance of yelp to google + +# Load the file from http://censusdata.ire.org/36/all_050_in_36.P2.csv and find +# the max P002001 diff --git a/lab-07/TEXT b/lab-07/TEXT new file mode 100644 index 0000000..41345ac --- /dev/null +++ b/lab-07/TEXT @@ -0,0 +1,4 @@ +[default] +aws_access_key_id = AKIAJDXXYNQ7ZH4LS76Q +aws_secret_access_key = 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 +region = ca-central-1 \ No newline at end of file diff --git a/lab-07/assign2.py b/lab-07/assign2.py new file mode 100644 index 0000000..7d045e2 --- /dev/null +++ b/lab-07/assign2.py @@ -0,0 +1,20 @@ +import boto3 + +def create_instance(): + ec2_resource = boto3.resource('ec2') + instances = ec2_resource.create_instances(ImageId='ami-6871a115', + MinCount=1, MaxCount=3,InstanceType='t2.micro', + SecurityGroupIds=['launch-wizard-7'],KeyName='fullstack') + + instancelists =[] + for instance in instances.items(): + print instance + + instancelists = instance.append('id') + + ec2_client = boto3.client('ec2') + waiter=ec2_client.get_waiter('instance_running') + waiter.wait(InstanceIds=[instances[0].id]) + print ("Instance is Running now!") + +create_instance() diff --git a/lab-07/ec-create-instance-3x.py b/lab-07/ec-create-instance-3x.py new file mode 100644 index 0000000..79ce448 --- /dev/null +++ b/lab-07/ec-create-instance-3x.py @@ -0,0 +1,31 @@ +import boto3 + + +def create_instance(): + ec2_resource = boto3.resource('ec2') + instances = ec2_resource.create_instances(ImageId='ami-6871a115', + MinCount=1, MaxCount=3,InstanceType='t2.micro', + SecurityGroupIds=['launch-wizard-7'],KeyName='fullstack') + instance_ids = [] + for instance in instances: + instance_ids.append(instance.id) + #print instance + ec2_client = boto3.client('ec2') + waiter=ec2_client.get_waiter('instance_running') + waiter.wait(InstanceIds=instance_ids) + print ("Instance is Running now!") + +create_instance() + +#print instances +#def print_instance(): + # + # + +#delete instances +def delete_instance(): + for instance in instance_ids.items(): + ec2_resource.delete_instances(instance.id) +#ec2_client.terminate_instances(InstanceIds=instance_list,DryRun=False) + +delete_instance() diff --git a/lab-07/ec-create-instance.py b/lab-07/ec-create-instance.py index 733e299..1a0a253 100644 --- a/lab-07/ec-create-instance.py +++ b/lab-07/ec-create-instance.py @@ -3,9 +3,9 @@ def create_instance(): ec2_resource = boto3.resource('ec2') - instances = ec2_resource.create_instances(ImageId='ami-49f0762d', + instances = ec2_resource.create_instances(ImageId='ami-6871a115', MinCount=1, MaxCount=1,InstanceType='t2.micro', - SecurityGroupIds=['ansible-node'],KeyName='ansible') + SecurityGroupIds=['launch-wizard-7'],KeyName='fullstack') for instance in instances: print instance ec2_client = boto3.client('ec2') diff --git a/lab-07/ec2-list-volumes.py b/lab-07/ec2-list-volumes.py new file mode 100644 index 0000000..9c75948 --- /dev/null +++ b/lab-07/ec2-list-volumes.py @@ -0,0 +1,13 @@ +import boto3 + +def list_volumes(): + ec2_resource = boto3.resource('ec2') + + instances = ec2_resource.instances.filter( + Filters=[{'Name': 'instance-state-name', 'Values': ['running']}]) + for instance in instances: + print(instance.state) + for item in instance.volumes.all(): + print item.id + +list_volumes() \ No newline at end of file diff --git a/lab-07/ec2-resource-list-instances.py b/lab-07/ec2-resource-list-instances.py index 5e20645..2ae7015 100644 --- a/lab-07/ec2-resource-list-instances.py +++ b/lab-07/ec2-resource-list-instances.py @@ -6,10 +6,13 @@ instances = ec2_resource.instances.filter( Filters=[{'Name': 'instance-state-name', 'Values': ['running']}]) for instance in instances: - print(instance.state) - for item in instance.volumes.all(): - print item.id + #print(instance.state) + print(instance.id,instance.volumes) + + # for item in instance.volumes.all(): + # print item.id + #list_volumes() # Mac installation tip: # sudo pip install --ignore-installed six boto3 diff --git a/lab-07/ec2-start-stop.py b/lab-07/ec2-start-stop.py new file mode 100644 index 0000000..bc2ecb7 --- /dev/null +++ b/lab-07/ec2-start-stop.py @@ -0,0 +1,41 @@ +import sys +import boto3 +from botocore.exceptions import ClientError + + +instance_id = sys.argv[2] +action = sys.argv[1] + +ec2 = boto3.client('ec2') + + +if action == 'start': + # Do a dryrun first to verify permissions + try: + ec2.start_instances(InstanceIds=[instance_id], DryRun=True) + except ClientError as e: + if 'DryRunOperation' not in str(e): + raise + + # Dry run succeeded, run start_instances without dryrun + try: + response = ec2.start_instances(InstanceIds=[instance_id], DryRun=False) + print(response) + except ClientError as e: + print(e) +elif action == 'stop': + # Do a dryrun first to verify permissions + try: + ec2.stop_instances(InstanceIds=[instance_id], DryRun=True) + except ClientError as e: + if 'DryRunOperation' not in str(e): + raise + + # Dry run succeeded, call stop_instances without dryrun + try: + response = ec2.stop_instances(InstanceIds=[instance_id], DryRun=False) + print(response) + except ClientError as e: + print(e) +else: + print( "Operation not supported") \ No newline at end of file diff --git a/lab-07/ec2-term-instances.py b/lab-07/ec2-term-instances.py new file mode 100644 index 0000000..65358d3 --- /dev/null +++ b/lab-07/ec2-term-instances.py @@ -0,0 +1,30 @@ +import boto3 +from botocore.exceptions import ClientError + + +def prepare_instance_list(): + mylist = [] + ec2_resource = boto3.resource('ec2') + instances = ec2_resource.instances.filter( + Filters=[{'Name': 'instance-state-name', 'Values': ['running']}]) + for instance in instances: + print(instance.id, instance.instance_type) + mylist.append(instance.id) + return mylist + +def delete_instances(instance_list): + ec2_client = boto3.client('ec2') + try: + ec2_client.terminate_instances(InstanceIds=instance_list,DryRun=True) + except ClientError as e: + if 'DryRunOperation' not in str(e): + raise + + ec2_client.terminate_instances(InstanceIds=instance_list,DryRun=False) + +instance_list = prepare_instance_list() +if len(instance_list) !=0: + print ( "Deleting Instances", instance_list) + delete_instances(instance_list) +else: + print ( "No Instances to delete ") \ No newline at end of file diff --git a/lab-07/ec2-terminate-instances.py b/lab-07/ec2-terminate-instances.py new file mode 100644 index 0000000..65358d3 --- /dev/null +++ b/lab-07/ec2-terminate-instances.py @@ -0,0 +1,30 @@ +import boto3 +from botocore.exceptions import ClientError + + +def prepare_instance_list(): + mylist = [] + ec2_resource = boto3.resource('ec2') + instances = ec2_resource.instances.filter( + Filters=[{'Name': 'instance-state-name', 'Values': ['running']}]) + for instance in instances: + print(instance.id, instance.instance_type) + mylist.append(instance.id) + return mylist + +def delete_instances(instance_list): + ec2_client = boto3.client('ec2') + try: + ec2_client.terminate_instances(InstanceIds=instance_list,DryRun=True) + except ClientError as e: + if 'DryRunOperation' not in str(e): + raise + + ec2_client.terminate_instances(InstanceIds=instance_list,DryRun=False) + +instance_list = prepare_instance_list() +if len(instance_list) !=0: + print ( "Deleting Instances", instance_list) + delete_instances(instance_list) +else: + print ( "No Instances to delete ") \ No newline at end of file diff --git a/lab-07/get-dns.py b/lab-07/get-dns.py new file mode 100644 index 0000000..a941c74 --- /dev/null +++ b/lab-07/get-dns.py @@ -0,0 +1,16 @@ +import boto3 + +ec2_client = boto3.client('ec2') +response = ec2_client.describe_instances() +print(response) + +#splitresponse + +#PublicDnsName' +for k,v in response.items(): + if k == 'Reservations': + for instance in v: + for i,vv in instance.items(): + if i == 'Instances': + for ii in vv: + print ii['PublicDnsName'] \ No newline at end of file