zaws 0.0.5 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/.travis.yml +9 -18
- data/Gemfile +0 -0
- data/Gemfile.lock +26 -35
- data/Gemfile.lock.linux +69 -0
- data/Gemfile.lock.mac +81 -0
- data/Gemfile.lock.windows +77 -0
- data/LICENSE +0 -0
- data/README.md +11 -0
- data/Rakefile +1 -7
- data/archive/cloud_trail/cloud_trail.feature +124 -0
- data/bin/win_zaws.bat +2 -0
- data/lib/zaws.rb +54 -41
- data/lib/zaws/command/ai.rb +34 -0
- data/lib/zaws/command/bucket.rb +35 -0
- data/lib/zaws/command/cloud_trail.rb +43 -0
- data/lib/zaws/command/compute.rb +152 -87
- data/lib/zaws/command/config.rb +42 -0
- data/lib/zaws/command/elasticip.rb +19 -9
- data/lib/zaws/command/hosted_zone.rb +31 -18
- data/lib/zaws/command/iam.rb +37 -0
- data/lib/zaws/command/load_balancer.rb +54 -51
- data/lib/zaws/command/nessus.rb +41 -0
- data/lib/zaws/command/newrelic.rb +30 -0
- data/lib/zaws/command/route_table.rb +135 -122
- data/lib/zaws/command/security_group.rb +95 -82
- data/lib/zaws/command/subnet.rb +58 -46
- data/lib/zaws/command/sumo.rb +34 -0
- data/lib/zaws/command/vpc.rb +53 -0
- data/lib/zaws/external/awscli/awscli.rb +65 -0
- data/lib/zaws/external/awscli/commands.rb +10 -0
- data/lib/zaws/external/awscli/commands/aws.rb +40 -0
- data/lib/zaws/external/awscli/commands/ec2.rb +74 -0
- data/lib/zaws/external/awscli/commands/ec2/allocate_address.rb +42 -0
- data/lib/zaws/external/awscli/commands/ec2/assign_private_ip_addresses.rb +49 -0
- data/lib/zaws/external/awscli/commands/ec2/associate_address.rb +48 -0
- data/lib/zaws/external/awscli/commands/ec2/associate_route_table.rb +48 -0
- data/lib/zaws/external/awscli/commands/ec2/authorize_security_group_ingress.rb +70 -0
- data/lib/zaws/external/awscli/commands/ec2/create_route.rb +60 -0
- data/lib/zaws/external/awscli/commands/ec2/create_route_table.rb +40 -0
- data/lib/zaws/external/awscli/commands/ec2/create_security_group.rb +58 -0
- data/lib/zaws/external/awscli/commands/ec2/create_subnet.rb +62 -0
- data/lib/zaws/external/awscli/commands/ec2/create_tags.rb +53 -0
- data/lib/zaws/external/awscli/commands/ec2/create_vpc.rb +21 -0
- data/lib/zaws/external/awscli/commands/ec2/delete_route.rb +54 -0
- data/lib/zaws/external/awscli/commands/ec2/delete_route_table.rb +46 -0
- data/lib/zaws/external/awscli/commands/ec2/delete_security_group.rb +46 -0
- data/lib/zaws/external/awscli/commands/ec2/delete_subnet.rb +46 -0
- data/lib/zaws/external/awscli/commands/ec2/describe_addresses.rb +41 -0
- data/lib/zaws/external/awscli/commands/ec2/describe_images.rb +58 -0
- data/lib/zaws/external/awscli/commands/ec2/describe_instances.rb +56 -0
- data/lib/zaws/external/awscli/commands/ec2/describe_route_tables.rb +44 -0
- data/lib/zaws/external/awscli/commands/ec2/describe_security_groups.rb +50 -0
- data/lib/zaws/external/awscli/commands/ec2/describe_subnets.rb +48 -0
- data/lib/zaws/external/awscli/commands/ec2/describe_vpc_peering_connections.rb +23 -0
- data/lib/zaws/external/awscli/commands/ec2/describe_vpcs.rb +26 -0
- data/lib/zaws/external/awscli/commands/ec2/disable_vgw_route_propagation.rb +48 -0
- data/lib/zaws/external/awscli/commands/ec2/disassociate-address.rb +42 -0
- data/lib/zaws/external/awscli/commands/ec2/disassociate_route_table.rb +41 -0
- data/lib/zaws/external/awscli/commands/ec2/enable_vgw_route_propagation.rb +48 -0
- data/lib/zaws/external/awscli/commands/ec2/filter.rb +115 -0
- data/lib/zaws/external/awscli/commands/ec2/modify_instance_attribute.rb +62 -0
- data/lib/zaws/external/awscli/commands/ec2/network_interfaces.rb +63 -0
- data/lib/zaws/external/awscli/commands/ec2/release_address.rb +42 -0
- data/lib/zaws/external/awscli/commands/ec2/revoke_security_group_ingress.rb +70 -0
- data/lib/zaws/external/awscli/commands/ec2/run_instances.rb +21 -0
- data/lib/zaws/external/awscli/commands/ec2/stop_instances.rb +21 -0
- data/lib/zaws/external/awscli/commands/ec2/terminate_instances.rb +40 -0
- data/lib/zaws/external/awscli/commands/ec2/unassign_private_ip_addresses.rb +49 -0
- data/lib/zaws/external/awscli/commands/elb.rb +19 -0
- data/lib/zaws/external/awscli/commands/elb/create_load_balancer.rb +64 -0
- data/lib/zaws/external/awscli/commands/elb/create_load_balancer_listeners.rb +47 -0
- data/lib/zaws/external/awscli/commands/elb/delete_load_balancer.rb +40 -0
- data/lib/zaws/external/awscli/commands/elb/delete_load_balancer_listeners.rb +49 -0
- data/lib/zaws/external/awscli/commands/elb/deregister_instances_with_load_balancer.rb +49 -0
- data/lib/zaws/external/awscli/commands/elb/describe_load_balancers.rb +30 -0
- data/lib/zaws/external/awscli/commands/elb/register_instances_with_load_balancer.rb +49 -0
- data/lib/zaws/external/awscli/commands/iam.rb +31 -0
- data/lib/zaws/external/awscli/commands/iam/GetPolicy.rb +21 -0
- data/lib/zaws/external/awscli/commands/iam/GetPolicyVersion.rb +21 -0
- data/lib/zaws/external/awscli/commands/iam/GetRolePolicy.rb +21 -0
- data/lib/zaws/external/awscli/commands/route53.rb +19 -0
- data/lib/zaws/external/awscli/commands/route53/list_hosted_zones.rb +30 -0
- data/lib/zaws/external/awscli/commands/route53/list_resource_record_sets.rb +41 -0
- data/lib/zaws/external/awscli/commands/s3/ls.rb +34 -0
- data/lib/zaws/external/awscli/commands/s3/mb.rb +41 -0
- data/lib/zaws/external/awscli/commands/s3/sync.rb +48 -0
- data/lib/zaws/external/awscli/credentials.rb +22 -0
- data/lib/zaws/external/awscli/data.rb +10 -0
- data/lib/zaws/external/awscli/data/ec2.rb +33 -0
- data/lib/zaws/external/awscli/data/ec2/instance.rb +162 -0
- data/lib/zaws/external/awscli/data/ec2/security_group.rb +51 -0
- data/lib/zaws/external/awscli/data/ec2/subnet.rb +59 -0
- data/lib/zaws/external/awscli/data/ec2/vpc.rb +85 -0
- data/lib/zaws/external/awscli/data/iam.rb +34 -0
- data/lib/zaws/external/awscli/data/iam/policy.rb +42 -0
- data/lib/zaws/external/awscli/data/iam/policy_document.rb +52 -0
- data/lib/zaws/external/awscli/data/iam/policy_version.rb +42 -0
- data/lib/zaws/external/awscli/data/iam/role_policy.rb +52 -0
- data/lib/zaws/external/awscli/generators/result/ec2/addresses.rb +70 -0
- data/lib/zaws/external/awscli/generators/result/ec2/allocation_id.rb +39 -0
- data/lib/zaws/external/awscli/generators/result/ec2/association_id.rb +29 -0
- data/lib/zaws/external/awscli/generators/result/ec2/images.rb +46 -0
- data/lib/zaws/external/awscli/generators/result/ec2/instances.rb +57 -0
- data/lib/zaws/external/awscli/generators/result/ec2/ip_permissions.rb +70 -0
- data/lib/zaws/external/awscli/generators/result/ec2/network_interfaces.rb +53 -0
- data/lib/zaws/external/awscli/generators/result/ec2/private_ip_addresses.rb +41 -0
- data/lib/zaws/external/awscli/generators/result/ec2/route_tables.rb +80 -0
- data/lib/zaws/external/awscli/generators/result/ec2/routes.rb +58 -0
- data/lib/zaws/external/awscli/generators/result/ec2/security_groups.rb +73 -0
- data/lib/zaws/external/awscli/generators/result/ec2/subnets.rb +88 -0
- data/lib/zaws/external/awscli/generators/result/ec2/tags.rb +34 -0
- data/lib/zaws/external/awscli/generators/result/ec2/virtual_gateway.rb +32 -0
- data/lib/zaws/external/awscli/generators/result/elb/listeners.rb +65 -0
- data/lib/zaws/external/awscli/generators/result/elb/load_balancers.rb +61 -0
- data/lib/zaws/external/awscli/generators/result/route53/hosted_zones.rb +52 -0
- data/lib/zaws/external/awscli/regions.rb +13 -0
- data/lib/zaws/external/nessusapi/data/agents.rb +36 -0
- data/lib/zaws/external/nessusapi/data/scanners.rb +35 -0
- data/lib/zaws/external/nessusapi/nessusapi.rb +57 -0
- data/lib/zaws/external/nessusapi/reources.rb +10 -0
- data/lib/zaws/external/nessusapi/resources/agents.rb +18 -0
- data/lib/zaws/external/nessusapi/resources/agents/list.rb +20 -0
- data/lib/zaws/external/nessusapi/resources/scanners.rb +18 -0
- data/lib/zaws/external/nessusapi/resources/scanners/list.rb +20 -0
- data/lib/zaws/external/newrelicapi/data/servers.rb +36 -0
- data/lib/zaws/external/newrelicapi/newrelic_client.rb +64 -0
- data/lib/zaws/external/newrelicapi/newrelic_creds.rb +60 -0
- data/lib/zaws/external/newrelicapi/newrelicapi.rb +46 -0
- data/lib/zaws/external/newrelicapi/resources/servers.rb +18 -0
- data/lib/zaws/external/newrelicapi/resources/servers/list.rb +20 -0
- data/lib/zaws/external/sumoapi/data/collectors.rb +35 -0
- data/lib/zaws/external/sumoapi/data/sources.rb +35 -0
- data/lib/zaws/external/sumoapi/resources/collectors.rb +18 -0
- data/lib/zaws/external/sumoapi/resources/collectors/list.rb +20 -0
- data/lib/zaws/external/sumoapi/resources/sources.rb +18 -0
- data/lib/zaws/external/sumoapi/resources/sources/list.rb +20 -0
- data/lib/zaws/external/sumoapi/sumo_client.rb +63 -0
- data/lib/zaws/external/sumoapi/sumo_creds.rb +67 -0
- data/lib/zaws/external/sumoapi/sumoapi.rb +56 -0
- data/lib/zaws/helper/data_lattice.rb +22 -0
- data/lib/zaws/helper/filestore.rb +48 -0
- data/lib/zaws/helper/filter.rb +15 -0
- data/lib/zaws/helper/inifile.rb +632 -0
- data/lib/zaws/helper/nessus_client.rb +143 -0
- data/lib/zaws/helper/nessus_creds.rb +67 -0
- data/lib/zaws/helper/option.rb +14 -15
- data/lib/zaws/helper/output.rb +81 -29
- data/lib/zaws/helper/process_hash.rb +47 -0
- data/lib/zaws/helper/shell.rb +0 -0
- data/lib/zaws/helper/verbose.rb +15 -0
- data/lib/zaws/helper/zfile.rb +8 -8
- data/lib/zaws/services/ai.rb +35 -0
- data/lib/zaws/services/ai/query.rb +135 -0
- data/lib/zaws/services/aws.rb +41 -0
- data/lib/zaws/services/chef.rb +0 -0
- data/lib/zaws/services/cloud_trail.rb +76 -0
- data/lib/zaws/services/config.rb +31 -0
- data/lib/zaws/services/ec2.rb +47 -0
- data/lib/zaws/services/ec2/compute.rb +352 -0
- data/lib/zaws/services/ec2/elasticip.rb +82 -0
- data/lib/zaws/services/ec2/route_table.rb +210 -0
- data/lib/zaws/services/ec2/security_group.rb +186 -0
- data/lib/zaws/services/ec2/subnet.rb +111 -0
- data/lib/zaws/services/ec2/vpc.rb +69 -0
- data/lib/zaws/{elb.rb → services/elb.rb} +3 -2
- data/lib/zaws/services/elb/load_balancer.rb +160 -0
- data/lib/zaws/services/iam.rb +25 -0
- data/lib/zaws/services/iam/policy.rb +25 -0
- data/lib/zaws/services/iam/role.rb +23 -0
- data/lib/zaws/services/nessus.rb +24 -0
- data/lib/zaws/services/nessus/agents.rb +19 -0
- data/lib/zaws/services/nessus/scanners.rb +19 -0
- data/lib/zaws/services/newrelic.rb +21 -0
- data/lib/zaws/services/newrelic/servers.rb +19 -0
- data/lib/zaws/{route53.rb → services/route53.rb} +1 -1
- data/lib/zaws/services/route53/hosted_zone.rb +36 -0
- data/lib/zaws/services/s3.rb +15 -0
- data/lib/zaws/services/s3/bucket.rb +42 -0
- data/lib/zaws/services/sumo.rb +21 -0
- data/lib/zaws/services/sumo/collectors.rb +19 -0
- data/lib/zaws/services/vmware.rb +0 -0
- data/lib/zaws/version.rb +1 -1
- data/shove_all.sh +6 -0
- data/shove_all_mac.sh +6 -0
- data/shove_all_windows.sh +6 -0
- data/spec/spec_helper.rb +0 -0
- data/spec/zaws/command/ec2/compute/assoc_security_group_spec.rb +135 -0
- data/spec/zaws/command/ec2/compute/compute_spec.rb +321 -0
- data/spec/zaws/command/ec2/compute/images_spec.rb +69 -0
- data/spec/zaws/command/ec2/compute/management_spec.rb +364 -0
- data/spec/zaws/command/ec2/compute/network_spec.rb +93 -0
- data/spec/zaws/command/ec2/compute/node_spec.rb +70 -0
- data/spec/zaws/command/ec2/compute/secondary_ip_spec.rb +212 -0
- data/spec/zaws/command/ec2/compute/storage_spec.rb +63 -0
- data/spec/zaws/command/ec2/elasticip_spec.rb +271 -0
- data/spec/zaws/command/ec2/route_table_spec.rb +844 -0
- data/spec/zaws/command/ec2/security_group_spec.rb +710 -0
- data/spec/zaws/command/ec2/subnet_spec.rb +273 -0
- data/spec/zaws/command/ec2/vpc_spec.rb +180 -0
- data/spec/zaws/command/elb/load_balancer_spec.rb +633 -0
- data/spec/zaws/command/iam/policy_spec.rb +101 -0
- data/spec/zaws/command/iam/role_policy_spec.rb +35 -0
- data/spec/zaws/command/route53/hosted_zone_spec.rb +100 -0
- data/spec/zaws/command/s3/bucket_spec.rb +70 -0
- data/spec/zaws/external/awscli/data/ec2/instance_spec.rb +90 -0
- data/spec/zaws/external/awscli/data/iam/policy_document_spec.rb +56 -0
- data/spec/zaws/external/awscli/data/iam/role_policy_spec.rb +54 -0
- data/spec/zaws/external/awscli/get_aws_version_spec.rb +18 -0
- data/spec/zaws/helper/option_spec.rb +54 -0
- data/spec/zaws/helper/output_spec.rb +144 -0
- data/spec/zaws/helper/process_hash_spec.rb +48 -0
- data/spec/zaws/helper/shell_spec.rb +56 -0
- data/spec/zaws/nessus/scanners.json +0 -0
- data/spec/zaws/version_spec.rb +18 -0
- data/zaws.gemspec +1 -3
- metadata +260 -189
- data/feature/compute/assoc_security_group.feature +0 -55
- data/feature/compute/compute.feature +0 -138
- data/feature/compute/secondary_ip.feature +0 -107
- data/feature/compute/view.feature +0 -23
- data/feature/compute/view_images.feature +0 -24
- data/feature/elasticip/elasticip.feature +0 -138
- data/feature/elasticip/view.feature +0 -18
- data/feature/hosted_zone/view.feature +0 -17
- data/feature/hosted_zone/view_record.feature +0 -29
- data/feature/load_balancer/instance_registration.feature +0 -120
- data/feature/load_balancer/listener.feature +0 -86
- data/feature/load_balancer/load_balancer.feature +0 -101
- data/feature/load_balancer/view.feature +0 -18
- data/feature/route_table/assoc_subnet.feature +0 -128
- data/feature/route_table/route_propagation.feature +0 -93
- data/feature/route_table/route_table.feature +0 -91
- data/feature/route_table/route_to_gateway.feature +0 -69
- data/feature/route_table/route_to_instance.feature +0 -115
- data/feature/route_table/view.feature +0 -25
- data/feature/security_group/ingress_cidr.feature +0 -144
- data/feature/security_group/ingress_group.feature +0 -184
- data/feature/security_group/security_group.feature +0 -107
- data/feature/security_group/view.feature +0 -23
- data/feature/subnet/subnet.feature +0 -94
- data/feature/subnet/view.feature +0 -24
- data/feature/support/env.rb +0 -14
- data/feature/version.feature +0 -6
- data/lib/zaws/aws.rb +0 -26
- data/lib/zaws/ec2.rb +0 -40
- data/lib/zaws/ec2/compute.rb +0 -247
- data/lib/zaws/ec2/elasticip.rb +0 -79
- data/lib/zaws/ec2/route_table.rb +0 -207
- data/lib/zaws/ec2/security_group.rb +0 -154
- data/lib/zaws/ec2/subnet.rb +0 -109
- data/lib/zaws/elb/load_balancer.rb +0 -157
- data/lib/zaws/route53/hosted_zone.rb +0 -36
- data/spec/zaws/ec2/compute/add_volume_spec.rb +0 -39
- data/spec/zaws/ec2/compute/block_device_mapping_spec.rb +0 -31
- data/spec/zaws/ec2/compute/instance_id_by_external_id_spec.rb +0 -23
- data/spec/zaws/ec2/compute/instance_ping_spec.rb +0 -34
- data/spec/zaws/ec2/compute/instance_running_spec.rb +0 -47
- data/spec/zaws/ec2/compute/network_interface_json_spec.rb +0 -57
- data/spec/zaws/ec2/compute/nosdcheck_spec.rb +0 -17
- data/spec/zaws/ec2/compute/tag_instance_spec.rb +0 -21
- data/spec/zaws/ec2/security_group/id_by_name_spec.rb +0 -32
- data/spec/zaws/ec2/subnet/available_spec.rb +0 -22
- data/spec/zaws/ec2/subnet/declare_spec.rb +0 -31
- data/spec/zaws/ec2/subnet/exists_spec.rb +0 -33
- data/spec/zaws/ec2/subnet/id_array_by_cidrblock_array_spec.rb +0 -48
- data/spec/zaws/ec2/subnet/id_by_cidrblock_spec.rb +0 -35
- data/spec/zaws/ec2/subnet/id_by_ip_spec.rb +0 -42
- data/spec/zaws/ec2/subnet/view_spec.rb +0 -34
- data/spec/zaws/elb/load_balancer/calculated_listener_spec.rb +0 -18
- data/spec/zaws/helper/option/absent_spec.rb +0 -14
- data/spec/zaws/helper/option/exclusive_spec.rb +0 -14
- data/spec/zaws/helper/option/exists_spec.rb +0 -18
- data/spec/zaws/helper/option/minimum_spec.rb +0 -14
- data/spec/zaws/helper/output/binary_nagios_check_spec.rb +0 -19
- data/spec/zaws/helper/output/colorize_spec.rb +0 -30
- data/spec/zaws/helper/output/opt_exclusive_spec.rb +0 -14
- data/spec/zaws/helper/output/opt_minimum_spec.rb +0 -15
- data/spec/zaws/helper/output/opt_required_spec.rb +0 -12
- data/spec/zaws/helper/shell/cli_spec.rb +0 -33
- data/spec/zaws/helper/shell/if_then_spec.rb +0 -24
@@ -0,0 +1,35 @@
|
|
1
|
+
module ZAWS
|
2
|
+
module Controllers
|
3
|
+
class AI
|
4
|
+
|
5
|
+
def initialize(shellout, nessusapi,sumoapi,newrelicapi,awscli)
|
6
|
+
@shellout=shellout
|
7
|
+
@_nessusapi= nessusapi ? nessusapi : ZAWS::Nessusapi.new(@shellout)
|
8
|
+
@_sumoapi= sumoapi ? sumoapi : ZAWS::Sumoapi.new(@shellout)
|
9
|
+
@_newrelicapi= newrelicapi ? newrelicapi : ZAWS::Newrelicapi.new(@shellout)
|
10
|
+
@_awscli= awscli ? awscli : ZAWS::AWSCLI.new(@shellout)
|
11
|
+
end
|
12
|
+
|
13
|
+
def nessusapi
|
14
|
+
return @_nessusapi
|
15
|
+
end
|
16
|
+
|
17
|
+
def sumoapi
|
18
|
+
return @_sumoapi
|
19
|
+
end
|
20
|
+
|
21
|
+
def newrelicapi
|
22
|
+
return @_newrelicapi
|
23
|
+
end
|
24
|
+
|
25
|
+
def awscli
|
26
|
+
return @_awscli
|
27
|
+
end
|
28
|
+
|
29
|
+
def query
|
30
|
+
@_query ||= (ZAWS::Services::AI::Query.new(@shellout, self))
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,135 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
module ZAWS
|
4
|
+
module Services
|
5
|
+
module AI
|
6
|
+
class Query
|
7
|
+
|
8
|
+
def initialize(shellout, ai)
|
9
|
+
@shellout=shellout
|
10
|
+
@ai=ai
|
11
|
+
end
|
12
|
+
|
13
|
+
def kilo(n)
|
14
|
+
count = 0
|
15
|
+
while n >= 1024 and count < 4
|
16
|
+
n /= 1024.0
|
17
|
+
count += 1
|
18
|
+
end
|
19
|
+
format("%.2f", n) + %w(B KB MB GB TB)[count]
|
20
|
+
end
|
21
|
+
|
22
|
+
def all(home, verbose=false, value)
|
23
|
+
out=ZAWS::Helper::Verbose.output(verbose)
|
24
|
+
results = {}
|
25
|
+
value_array=[]
|
26
|
+
value_array.concat(value)
|
27
|
+
query_aws(value_array, out, results)
|
28
|
+
query_nessus(home, results, value_array, out)
|
29
|
+
query_sumo(home, results, value_array, out)
|
30
|
+
query_newrelic(home, results, value_array, out)
|
31
|
+
results=ZAWS::Helper::ProcessHash.keep(results,value_array)
|
32
|
+
results.to_yaml
|
33
|
+
end
|
34
|
+
|
35
|
+
def query_nessus(home, results, value_array, verbose)
|
36
|
+
@ai.nessusapi.home=home
|
37
|
+
nessusapi_details = @ai.nessusapi.data_agents.view(1, verbose)
|
38
|
+
results['nessus']= []
|
39
|
+
nessusapi_details['agents'].each do |x|
|
40
|
+
value_array.each do |value|
|
41
|
+
if x['ip'].include?(value) || x['name'].include?(value)
|
42
|
+
if x['last_scanned']
|
43
|
+
x['last_scanned']= x['last_scanned'] + " <--- #{DateTime.strptime(x['last_scanned'], '%s')}"
|
44
|
+
end
|
45
|
+
results['nessus'] << x
|
46
|
+
break
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def query_sumo(home, results, value_array, verbose)
|
53
|
+
@ai.sumoapi.home=home
|
54
|
+
sumoapi_details = @ai.sumoapi.data_collectors.view(verbose)
|
55
|
+
results['sumo']= []
|
56
|
+
sumoapi_details['collectors'].each do |x|
|
57
|
+
value_array.each do |value|
|
58
|
+
if x['name'].include?(value)
|
59
|
+
sumoapi_sources=@ai.sumoapi.data_sources.view(verbose, x['id'])
|
60
|
+
x['sources']=sumoapi_sources
|
61
|
+
results['sumo'] << x
|
62
|
+
break
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def query_aws(value, verbose, results)
|
69
|
+
profile_creds=ZAWS::AWSCLI::Credentials.new("#{@ai.awscli.home}/.aws/credentials")
|
70
|
+
item = []
|
71
|
+
profile_creds.profiles.each do |profile|
|
72
|
+
@ai.awscli.main_regions.each do |region|
|
73
|
+
filters= {}
|
74
|
+
@ai.awscli.command_ec2.describeInstances.execute(region, 'json', filters, nil, verbose, profile)
|
75
|
+
res = @ai.awscli.data_ec2.instance.view('hash')
|
76
|
+
res['profile']=profile
|
77
|
+
item << res
|
78
|
+
end
|
79
|
+
end
|
80
|
+
results['awscli']= []
|
81
|
+
item.each do |reservations|
|
82
|
+
reservations['Reservations'].each do |reservation|
|
83
|
+
reservation['Instances'].each do |instance|
|
84
|
+
found=false
|
85
|
+
found=true if instance['InstanceId'] and instance['InstanceId'].include?(value[0])
|
86
|
+
found=true if instance['PrivateIpAddress'] and instance['PrivateIpAddress'].include?(value[0])
|
87
|
+
if instance['Tags']
|
88
|
+
instance['Tags'].each do |tag|
|
89
|
+
if tag['Value'] and tag['Value'].include?(value[0])
|
90
|
+
found=true
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
if found
|
95
|
+
instance['profile']=reservations['profile']
|
96
|
+
results['awscli'] << instance
|
97
|
+
value << instance['InstanceId']
|
98
|
+
if instance['PrivateIpAddress']
|
99
|
+
value << instance['PrivateIpAddress'] unless instance['PrivateIpAddress'].include?(value[0])
|
100
|
+
value << instance['PrivateIpAddress'].gsub('.', '-') unless instance['PrivateIpAddress'].gsub('.', '-').include?(value[0])
|
101
|
+
end
|
102
|
+
if instance['Tags']
|
103
|
+
instance['Tags'].each do |tag|
|
104
|
+
if tag['Key'].equal?('Name')
|
105
|
+
value << tag['Value'] unless tag['Value'].include?(value[0])
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def query_newrelic(home, results, value_array, verbose)
|
116
|
+
@ai.newrelicapi.home=home
|
117
|
+
newrelicapi_details = @ai.newrelicapi.data_servers.view(verbose)
|
118
|
+
results['newrelic'] =[]
|
119
|
+
newrelicapi_details['servers'].each do |x|
|
120
|
+
value_array.each do |value|
|
121
|
+
if x['name'].include?(value)
|
122
|
+
if x['summary'] and x['summary']['memory_used']
|
123
|
+
x['summary']['memory_used']="#{x['summary']['memory_used']}"+" <--- #{kilo(x['summary']['memory_used'])}"
|
124
|
+
end
|
125
|
+
results['newrelic'] << x
|
126
|
+
break
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module ZAWS
|
2
|
+
class AWS
|
3
|
+
|
4
|
+
def initialize(shellout, awscli,undofile=nil)
|
5
|
+
@shellout=shellout
|
6
|
+
@_awscli= awscli ? awscli : ZAWS::AWSCLI.new(@shellout)
|
7
|
+
@undofile=undofile
|
8
|
+
end
|
9
|
+
|
10
|
+
def awscli
|
11
|
+
return @_awscli
|
12
|
+
end
|
13
|
+
|
14
|
+
def ec2
|
15
|
+
@_ec2 ||= (ZAWS::EC2.new(@shellout, self,@undofile))
|
16
|
+
end
|
17
|
+
|
18
|
+
def elb
|
19
|
+
@_elb ||= (ZAWS::ELB.new(@shellout, self,@undofile))
|
20
|
+
end
|
21
|
+
|
22
|
+
def route53
|
23
|
+
@_route53 ||= (ZAWS::Route53.new(@shellout, self))
|
24
|
+
end
|
25
|
+
|
26
|
+
def s3
|
27
|
+
@_s3 ||= (ZAWS::S3.new(@shellout, self))
|
28
|
+
end
|
29
|
+
|
30
|
+
def cloud_trail
|
31
|
+
@_cloud_trail ||= (ZAWS::CloudTrail.new(@shellout, self))
|
32
|
+
end
|
33
|
+
|
34
|
+
def iam
|
35
|
+
@_iam ||= (ZAWS::IAM.new(@shellout, self))
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
File without changes
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'digest/sha1'
|
3
|
+
require 'fileutils'
|
4
|
+
require 'zlib'
|
5
|
+
|
6
|
+
module ZAWS
|
7
|
+
class CloudTrail
|
8
|
+
DEFAULT_DAYS_TO_FETCH=7
|
9
|
+
ZAWS_S3_CACHE="#{Dir.home}/.zaws/s3-cache"
|
10
|
+
|
11
|
+
def initialize(shellout,aws)
|
12
|
+
@shellout=shellout
|
13
|
+
@aws=aws
|
14
|
+
end
|
15
|
+
|
16
|
+
def get_cloud_trail_by_bucket(region,bucket_name,as_raw=false,verbose=nil)
|
17
|
+
bucket_name = "s3://#{bucket_name}" if !bucket_name.match('s3://.*')
|
18
|
+
bucket_hash = Digest::SHA1.hexdigest("#{region}#{bucket_name}")
|
19
|
+
|
20
|
+
dir_name = "#{ZAWS_S3_CACHE}/#{bucket_hash}"
|
21
|
+
FileUtils.mkdir_p(dir_name)
|
22
|
+
|
23
|
+
dir_name = @aws.s3.bucket.sync(region,bucket_name,dir_name,verbose)
|
24
|
+
|
25
|
+
results = []
|
26
|
+
Dir.open(dir_name) { |dir|
|
27
|
+
Dir.glob(File.join(dir, '**', '*')) { |filename|
|
28
|
+
Zlib::GzipReader.open(filename) { |file|
|
29
|
+
log_file = JSON.parse file.read
|
30
|
+
results.push log_file['Records']
|
31
|
+
} if File.file? filename
|
32
|
+
}
|
33
|
+
}
|
34
|
+
json = {:Records => results.flatten(1)}.to_json
|
35
|
+
|
36
|
+
if as_raw
|
37
|
+
puts json
|
38
|
+
else
|
39
|
+
puts ZAWS::Helper::Output.cloudtrail(json)
|
40
|
+
end
|
41
|
+
|
42
|
+
json
|
43
|
+
end
|
44
|
+
|
45
|
+
def get_cloud_trail_by_name(region,trail_name,as_raw=false, verbose=nil)
|
46
|
+
available_cloud_trails = get_cloud_trails(region)
|
47
|
+
bucket_name = available_cloud_trails.find { |available_cloud_trail|
|
48
|
+
available_cloud_trail['Name'] === trail_name
|
49
|
+
}['S3BucketName']
|
50
|
+
|
51
|
+
get_cloud_trail_by_bucket(region, bucket_name, as_raw, verbose)
|
52
|
+
end
|
53
|
+
|
54
|
+
def get_cloud_trails(region, verbose=nil)
|
55
|
+
com_line = "aws cloudtrail describe-trails --region #{region}"
|
56
|
+
cloud_trails = JSON.parse @shellout.cli(com_line, verbose)
|
57
|
+
cloud_trails['trailList']
|
58
|
+
end
|
59
|
+
|
60
|
+
def exists(name,region)
|
61
|
+
get_cloud_trails(region).any? {|trail| trail['Name'] === name}
|
62
|
+
end
|
63
|
+
|
64
|
+
def declare(name,region,bucket_name,verbose=nil)
|
65
|
+
if exists(name,region)
|
66
|
+
puts "CloudTrail already exists. Creation skipped.\n"
|
67
|
+
else
|
68
|
+
bucket_exists=@aws.s3.bucket().exists(bucket_name,region)
|
69
|
+
cmdline = "aws --region #{region} cloudtrail create-subscription " <<
|
70
|
+
"--name #{name} --s3-#{bucket_exists ? 'use' : 'new'}-bucket #{bucket_name}"
|
71
|
+
puts @shellout.cli(cmdline,verbose)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module ZAWS
|
2
|
+
module Controllers
|
3
|
+
class Config
|
4
|
+
|
5
|
+
def initialize(shellout, nessusapi,sumoapi,newrelicapi,awscli)
|
6
|
+
@shellout=shellout
|
7
|
+
@_nessusapi= nessusapi ? nessusapi : ZAWS::Nessusapi.new(@shellout)
|
8
|
+
@_sumoapi= sumoapi ? sumoapi : ZAWS::Sumoapi.new(@shellout)
|
9
|
+
@_newrelicapi= newrelicapi ? newrelicapi : ZAWS::Newrelicapi.new(@shellout)
|
10
|
+
@_awscli= awscli ? awscli : ZAWS::AWSCLI.new(@shellout)
|
11
|
+
end
|
12
|
+
|
13
|
+
def nessusapi
|
14
|
+
return @_nessusapi
|
15
|
+
end
|
16
|
+
|
17
|
+
def sumoapi
|
18
|
+
return @_sumoapi
|
19
|
+
end
|
20
|
+
|
21
|
+
def newrelicapi
|
22
|
+
return @_newrelicapi
|
23
|
+
end
|
24
|
+
|
25
|
+
def awscli
|
26
|
+
return @_awscli
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'netaddr'
|
3
|
+
require 'timeout'
|
4
|
+
|
5
|
+
module ZAWS
|
6
|
+
class EC2
|
7
|
+
|
8
|
+
def initialize(shellout,aws,undofile=nil)
|
9
|
+
@shellout=shellout
|
10
|
+
@aws=aws
|
11
|
+
@undofile=undofile
|
12
|
+
end
|
13
|
+
|
14
|
+
def vpc
|
15
|
+
@_vpc ||= (ZAWS::Services::EC2::VPC.new(@shellout,@aws,@undofile))
|
16
|
+
|
17
|
+
return @_vpc
|
18
|
+
end
|
19
|
+
|
20
|
+
def subnet
|
21
|
+
@_subnet ||= (ZAWS::Services::EC2::Subnet.new(@shellout,@aws,@undofile))
|
22
|
+
return @_subnet
|
23
|
+
end
|
24
|
+
|
25
|
+
def security_group
|
26
|
+
@_security_group ||= (ZAWS::Services::EC2::SecurityGroup.new(@shellout,@aws,@undofile))
|
27
|
+
return @_security_group
|
28
|
+
end
|
29
|
+
|
30
|
+
def route_table
|
31
|
+
@_route_table ||= (ZAWS::Services::EC2::RouteTable.new(@shellout,@aws,@undofile))
|
32
|
+
return @_route_table
|
33
|
+
end
|
34
|
+
|
35
|
+
def compute
|
36
|
+
@_compute ||= (ZAWS::Services::EC2::Compute.new(@shellout,@aws,@undofile))
|
37
|
+
return @_compute
|
38
|
+
end
|
39
|
+
|
40
|
+
def elasticip
|
41
|
+
@_elasticip ||= (ZAWS::Services::EC2::Elasticip.new(@shellout,@aws,@undofile))
|
42
|
+
return @_elasticip
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
@@ -0,0 +1,352 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'netaddr'
|
3
|
+
require 'timeout'
|
4
|
+
|
5
|
+
module ZAWS
|
6
|
+
module Services
|
7
|
+
module EC2
|
8
|
+
class Compute
|
9
|
+
|
10
|
+
def initialize(shellout, aws,undofile)
|
11
|
+
@shellout=shellout
|
12
|
+
@aws=aws
|
13
|
+
@undofile=undofile
|
14
|
+
@undofile ||= ZAWS::Helper::ZFile.new
|
15
|
+
end
|
16
|
+
|
17
|
+
def view(region, viewtype, textout=nil, verbose=nil, vpcid=nil, externalid=nil,profile=nil,home=nil)
|
18
|
+
# comline="aws --output #{viewtype} --region #{region} ec2 describe-instances"
|
19
|
+
# if vpcid || externalid
|
20
|
+
# comline = comline + " --filter"
|
21
|
+
# end
|
22
|
+
# comline = comline + " \"Name=vpc-id,Values=#{vpcid}\"" if vpcid
|
23
|
+
# comline = comline + " \"Name=tag:externalid,Values=#{externalid}\"" if externalid
|
24
|
+
# instances=@shellout.cli(comline, verbose)
|
25
|
+
# textout.puts(instances) if textout
|
26
|
+
# return instances
|
27
|
+
filters= {}
|
28
|
+
filters['vpc-id']=vpcid if vpcid
|
29
|
+
filters['tag:externalid']=externalid if externalid
|
30
|
+
view=viewtype=='yaml'? 'json':viewtype
|
31
|
+
@aws.awscli.home=home
|
32
|
+
@aws.awscli.command_ec2.describeInstances.execute(region,view ,filters, textout, verbose,profile)
|
33
|
+
instances = @aws.awscli.data_ec2.instance.view(viewtype)
|
34
|
+
textout.puts(instances) if textout
|
35
|
+
return instances
|
36
|
+
end
|
37
|
+
|
38
|
+
def view_images(region, viewtype, owner, imageid, textout=nil, verbose=nil)
|
39
|
+
comline="aws --output #{viewtype} --region #{region} ec2 describe-images"
|
40
|
+
comline = "#{comline} --owner #{owner}" if owner
|
41
|
+
comline = "#{comline} --image-ids #{imageid}" if imageid
|
42
|
+
images=@shellout.cli(comline, verbose)
|
43
|
+
textout.puts(images) if textout
|
44
|
+
return images
|
45
|
+
end
|
46
|
+
|
47
|
+
def exists(region, textout=nil, verbose=nil, vpcid, externalid)
|
48
|
+
instances=JSON.parse(view(region, 'json', nil, verbose, vpcid, externalid))
|
49
|
+
val = (instances["Reservations"].count == 1) && (instances["Reservations"][0]["Instances"].count == 1)
|
50
|
+
instance_id = val ? instances["Reservations"][0]["Instances"][0]["InstanceId"] : nil
|
51
|
+
sgroups = val ? instances["Reservations"][0]["Instances"][0]["SecurityGroups"] : nil
|
52
|
+
textout.puts val.to_s if textout
|
53
|
+
return val, instance_id, sgroups
|
54
|
+
end
|
55
|
+
|
56
|
+
def instance_id_by_external_id(region, externalid, vpcid=nil, textout=nil, verbose=nil)
|
57
|
+
val, instance_id, sgroups=exists(region, nil, verbose, vpcid, externalid)
|
58
|
+
return instance_id
|
59
|
+
end
|
60
|
+
|
61
|
+
def network_interface_json(region, verbose, vpcid, ip, groupname)
|
62
|
+
ec2_dir = File.dirname(__FILE__)
|
63
|
+
ip_to_subnet_id = @aws.ec2.subnet.id_by_ip(region, verbose, vpcid, ip)
|
64
|
+
subnet_id=ip_to_subnet_id
|
65
|
+
security_group_id= @aws.ec2.security_group.id_by_name(region, nil, verbose, vpcid, groupname)
|
66
|
+
new_hash= [{"Groups" => [security_group_id], "PrivateIpAddress" => "#{ip}", "DeviceIndex" => 0, "SubnetId" => ip_to_subnet_id}]
|
67
|
+
return new_hash.to_json
|
68
|
+
end
|
69
|
+
|
70
|
+
def block_device_mapping(region, owner, verbose, root_size, image_id)
|
71
|
+
image_descriptions=JSON.parse(view_images(region, 'json', owner, image_id, nil, verbose))
|
72
|
+
image_mappings=image_descriptions['Images'][0]["BlockDeviceMappings"]
|
73
|
+
image_root=image_descriptions['Images'][0]["RootDeviceName"]
|
74
|
+
image_mappings.each do |x|
|
75
|
+
if x["DeviceName"]==image_root
|
76
|
+
if x["Ebs"]["VolumeSize"].to_i > root_size.to_i
|
77
|
+
raise "The image root size is greater than the specified root size. image=#{x["Ebs"]["VolumeSize"]} > rootsize=#{root_size}"
|
78
|
+
exit 1
|
79
|
+
end
|
80
|
+
x["Ebs"]["VolumeSize"]=root_size.to_i
|
81
|
+
#You cannot specify the encrypted flag if specifying a snapshot id in a block device mapping. -AWS
|
82
|
+
x["Ebs"].delete("Encrypted") if x["Ebs"]["SnapshotId"]
|
83
|
+
end
|
84
|
+
end
|
85
|
+
return image_mappings.to_json
|
86
|
+
end
|
87
|
+
|
88
|
+
def random_clienttoken
|
89
|
+
(0...8).map { (65 + rand(26)).chr }.join
|
90
|
+
end
|
91
|
+
|
92
|
+
def placement_aggregate(zone, tenancy)
|
93
|
+
aggregate_value=[]
|
94
|
+
aggregate_value << "AvailabilityZone=#{zone}" if zone
|
95
|
+
aggregate_value << "Tenancy=#{tenancy}" if tenancy
|
96
|
+
aggregate_value.join(",")
|
97
|
+
end
|
98
|
+
|
99
|
+
def declare(externalid, image, owner, nodetype, root, zone, key, sgroup, privateip, optimized, apiterminate, clienttoken, region, textout, verbose, vpcid, nagios, ufile, no_sdcheck, skip_running_check, volsize, volume, tenancy, profilename, userdata)
|
100
|
+
if ufile
|
101
|
+
@undofile.prepend("zaws compute delete #{externalid} --region #{region} --vpcid #{vpcid} $XTRA_OPTS", '#Delete instance', ufile)
|
102
|
+
end
|
103
|
+
compute_exists, instance_id, sgroups = exists(region, nil, verbose, vpcid, externalid)
|
104
|
+
return ZAWS::Helper::Output.binary_nagios_check(compute_exists, "OK: Instance already exists.", "CRITICAL: Instance does not exist.", textout) if nagios
|
105
|
+
if not compute_exists
|
106
|
+
clienttoken=random_clienttoken if not clienttoken
|
107
|
+
comline = "aws --region #{region} ec2 run-instances --image-id #{image} --key-name #{key} --instance-type #{nodetype}"
|
108
|
+
#comline = comline + " --user-data 'file://#{options[:userdata]}'" if options[:userdata]
|
109
|
+
comline = comline + " --placement #{placement_aggregate(zone, tenancy)}" if zone or tenancy
|
110
|
+
comline = comline + " --block-device-mappings \"#{block_device_mapping(region, owner, verbose, root, image).gsub("\"","\\\"")}\"" if root
|
111
|
+
comline = apiterminate ? comline + " --enable-api-termination" : comline + " --disable-api-termination"
|
112
|
+
comline = comline + " --client-token #{clienttoken}"
|
113
|
+
comline = comline + " --network-interfaces \"#{network_interface_json(region, verbose, vpcid, privateip[0], sgroup).gsub("\"","\\\"")}\"" if privateip # Difference between vpc and classic
|
114
|
+
#comline = comline + " --security-groups '#{options[:securitygroup]}'" if not options[:privateip]
|
115
|
+
comline = comline + " --iam-instance-profile Name=\"#{profilename}\"" if profilename
|
116
|
+
comline = comline + " --user-data \"file://#{userdata}\"" if userdata
|
117
|
+
|
118
|
+
comline = optimized ? comline + " --ebs-optimized" : comline + " --no-ebs-optimized"
|
119
|
+
newinstance=JSON.parse(@shellout.cli(comline, verbose))
|
120
|
+
ZAWS::Helper::Output.out_change(textout, "Instance created.") if (newinstance["Instances"] and newinstance["Instances"][0]["InstanceId"])
|
121
|
+
new_instanceid=newinstance["Instances"][0]["InstanceId"]
|
122
|
+
tag_resource(region, new_instanceid, externalid, verbose)
|
123
|
+
instance_running?(region, vpcid, externalid, 60, 5, verbose) if not skip_running_check
|
124
|
+
add_volume(region, new_instanceid, externalid, privateip, volume, zone, volsize, verbose) if volume
|
125
|
+
nosdcheck(region, new_instanceid, verbose) if no_sdcheck # Needed for NAT instances.
|
126
|
+
else
|
127
|
+
ZAWS::Helper::Output.out_no_op(textout, "Instance already exists. Creation skipped.")
|
128
|
+
end
|
129
|
+
|
130
|
+
end
|
131
|
+
|
132
|
+
def delete(region, textout=nil, verbose=nil, vpcid, externalid)
|
133
|
+
compute_exists, instance_id, sgroups = exists(region, nil, verbose, vpcid, externalid)
|
134
|
+
if compute_exists
|
135
|
+
comline = "aws --region #{region} ec2 terminate-instances --instance-ids #{instance_id}"
|
136
|
+
delinstance=JSON.parse(@shellout.cli(comline, verbose))
|
137
|
+
ZAWS::Helper::Output.out_change(textout, "Instance deleted.") if delinstance["TerimatingInstances"]
|
138
|
+
else
|
139
|
+
ZAWS::Helper::Output.out_no_op(textout, "Instance does not exist. Skipping deletion.")
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
def exists_security_group_assoc(region, textout, verbose, vpcid, externalid, sgroup)
|
144
|
+
compute_exists, instance_id, sgroups = exists(region, nil, verbose, vpcid, externalid)
|
145
|
+
sgroup_exists, sgroupid = @aws.ec2.security_group.exists(region, verbose, vpcid, sgroup)
|
146
|
+
verbose.puts "compute_exists=#{compute_exists}" if verbose
|
147
|
+
verbose.puts "sgroup_exists=#{sgroup_exists}" if verbose
|
148
|
+
verbose.puts "sgroups=#{sgroups}" if verbose
|
149
|
+
if compute_exists and sgroup_exists
|
150
|
+
assoc_exists = sgroups.any? { |z| z["GroupId"] == "#{sgroupid}" }
|
151
|
+
textout.puts assoc_exists.to_s if textout
|
152
|
+
return assoc_exists, instance_id, sgroupid
|
153
|
+
else
|
154
|
+
textout.puts false if textout
|
155
|
+
return false, instance_id, sgroupid
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
def assoc_security_group(region, textout, verbose, vpcid, externalid, sgroup)
|
160
|
+
assoc_exists, instance_id, sgroupid=exists_security_group_assoc(region, nil, verbose, vpcid, externalid, sgroup)
|
161
|
+
if not assoc_exists
|
162
|
+
comline = "aws --region #{region} ec2 modify-instance-attribute --instance-id #{instance_id} --groups #{sgroupid}"
|
163
|
+
verbose.puts "comline=#{comline}" if verbose
|
164
|
+
assocsgroup=JSON.parse(@shellout.cli(comline, verbose))
|
165
|
+
ZAWS::Helper::Output.out_change(textout, "Security Group Association Changed.") if assocsgroup["return"]=="true"
|
166
|
+
else
|
167
|
+
ZAWS::Helper::Output.out_no_op(textout, "Security Group Association Not Changed.")
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
def tag_resource(region, resourceid, externalid, verbose=nil)
|
172
|
+
comline="aws --output json --region #{region} ec2 create-tags --resources #{resourceid} --tags \"Key=externalid,Value=#{externalid}\""
|
173
|
+
tag_creation=@shellout.cli(comline, verbose)
|
174
|
+
comline="aws --output json --region #{region} ec2 create-tags --resources #{resourceid} --tags \"Key=Name,Value=#{externalid}\""
|
175
|
+
tag_creation=@shellout.cli(comline, verbose)
|
176
|
+
end
|
177
|
+
|
178
|
+
def nosdcheck(region, instanceid, verbose=nil)
|
179
|
+
comline = "aws --output json --region #{region} ec2 modify-instance-attribute --instance-id #{instanceid} --no-source-dest-check"
|
180
|
+
nosdcheck_result=JSON.parse(@shellout.cli(comline, verbose))
|
181
|
+
end
|
182
|
+
|
183
|
+
def instance_ping?(ip, statetimeout, sleeptime, verbose=nil)
|
184
|
+
begin
|
185
|
+
Timeout.timeout(statetimeout) do
|
186
|
+
begin
|
187
|
+
comline ="ping -q -c 2 #{ip}"
|
188
|
+
@shellout.cli(comline, verbose)
|
189
|
+
rescue Mixlib::ShellOut::ShellCommandFailed
|
190
|
+
sleep(sleeptime)
|
191
|
+
retry
|
192
|
+
end
|
193
|
+
end
|
194
|
+
rescue Timeout::Error
|
195
|
+
raise StandardError.new('Timeout before instance responded to ping.')
|
196
|
+
end
|
197
|
+
return true
|
198
|
+
end
|
199
|
+
|
200
|
+
def instance_running?(region, vpcid, externalid, statetimeout, sleeptime, verbose=nil)
|
201
|
+
begin
|
202
|
+
Timeout.timeout(statetimeout) do
|
203
|
+
begin
|
204
|
+
sleep(sleeptime)
|
205
|
+
query_instance=JSON.parse(view(region, 'json', nil, verbose, vpcid, externalid))
|
206
|
+
end while query_instance["Reservations"][0]["Instances"][0]["State"]["Code"]!=16
|
207
|
+
end
|
208
|
+
rescue Timeout::Error
|
209
|
+
raise StandardError.new('Timeout before instance state code set to running(16).')
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
def add_volume(region, instanceid, externalid, ip, volume, zone, volsize, verbose=nil)
|
214
|
+
comline = "aws --output json --region #{region} ec2 create-volume --availability-zone #{zone} --size #{volsize}"
|
215
|
+
new_volume=JSON.parse(@shellout.cli(comline, verbose))
|
216
|
+
new_volumeid=new_volume["VolumeId"]
|
217
|
+
tag_resource(region, new_volumeid, externalid, verbose)
|
218
|
+
if instance_ping?(ip, 10, 1)
|
219
|
+
comline = "aws --output json ec2 attach-volume --region #{region} --volume-id #{new_volumeid} --instance-id #{instanceid} --device #{volume}"
|
220
|
+
volattach=JSON.parse(@shellout.cli(comline, verbose))
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
def exists_secondary_ip(region, ip, textout, verbose, vpcid, externalid)
|
225
|
+
compute_exists, instance_id, sgroups = exists(region, nil, verbose, vpcid, externalid)
|
226
|
+
if compute_exists
|
227
|
+
query_instance=JSON.parse(view(region, 'json', nil, verbose, vpcid, externalid))
|
228
|
+
val = query_instance["Reservations"][0]["Instances"][0]["NetworkInterfaces"][0]["PrivateIpAddresses"].any? { |x| x["PrivateIpAddress"] == "#{ip}" }
|
229
|
+
netid = query_instance["Reservations"][0]["Instances"][0]["NetworkInterfaces"][0]["NetworkInterfaceId"]
|
230
|
+
textout.puts val.to_s if textout
|
231
|
+
return val, true, netid
|
232
|
+
else
|
233
|
+
return false, false, nil
|
234
|
+
end
|
235
|
+
end
|
236
|
+
|
237
|
+
def declare_secondary_ip(region, ip, textout, verbose, vpcid, externalid, nagios, ufile)
|
238
|
+
if ufile
|
239
|
+
@undofile.prepend("zaws compute delete_secondary_ip #{externalid} #{ip} --region #{region} --vpcid #{vpcid} $XTRA_OPTS", '#Delete secondary ip', ufile)
|
240
|
+
end
|
241
|
+
compute_exists, instance_id, sgroups = exists(region, nil, verbose, vpcid, externalid)
|
242
|
+
secondary_ip_exists, compute_exists, network_interface = exists_secondary_ip(region, ip, nil, verbose, vpcid, externalid)
|
243
|
+
return ZAWS::Helper::Output.binary_nagios_check(secondary_ip_exists, "OK: Secondary ip exists.", "CRITICAL: Secondary ip does not exist.", textout) if nagios
|
244
|
+
if not secondary_ip_exists and compute_exists
|
245
|
+
comline = "aws --output json --region #{region} ec2 assign-private-ip-addresses --network-interface-id \"#{network_interface}\" --private-ip-addresses \"#{ip}\""
|
246
|
+
assignreturn = JSON.parse(@shellout.cli(comline, verbose))
|
247
|
+
ZAWS::Helper::Output.out_change(textout, "Secondary ip assigned.") if assignreturn["return"] == "true"
|
248
|
+
else
|
249
|
+
ZAWS::Helper::Output.out_no_op(textout, "Secondary ip already exists. Skipping assignment.")
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
def delete_secondary_ip(region, ip, textout, verbose, vpcid, externalid)
|
254
|
+
secondary_ip_exists, compute_exists, network_interface = exists_secondary_ip(region, ip, nil, verbose, vpcid, externalid)
|
255
|
+
if secondary_ip_exists and compute_exists
|
256
|
+
comline = "aws --output json --region #{region} ec2 unassign-private-ip-addresses --network-interface-id \"#{network_interface}\" --private-ip-addresses \"#{ip}\""
|
257
|
+
assignreturn = JSON.parse(@shellout.cli(comline, verbose))
|
258
|
+
ZAWS::Helper::Output.out_change(textout, "Secondary ip deleted.") if assignreturn["return"] == "true"
|
259
|
+
else
|
260
|
+
ZAWS::Helper::Output.out_no_op(textout, "Secondary IP does not exists, skipping deletion.")
|
261
|
+
end
|
262
|
+
end
|
263
|
+
|
264
|
+
def interval_eligible(policy_arn=nil, region, textout, verbose)
|
265
|
+
@aws.awscli.command_iam.getPolicy.execute(policy_arn, 'json', verbose)
|
266
|
+
version=@aws.awscli.data_iam.policy.defaultVersion
|
267
|
+
@aws.awscli.command_iam.getPolicyVersion.execute(policy_arn, version, 'json', verbose)
|
268
|
+
instanceids = @aws.awscli.data_iam.policy_document.resource_instance_ids()
|
269
|
+
@aws.awscli.command_ec2.describeInstances.execute(region, 'json', {}, textout, verbose)
|
270
|
+
instancenames = @aws.awscli.data_ec2.instance.names_by_ids(instanceids)
|
271
|
+
textout.puts(instancenames) if textout
|
272
|
+
end
|
273
|
+
|
274
|
+
def set_interval(policy_arn=nil, name=nil, externalid=nil, hours, email, region, textout, verbose, overridebasetime)
|
275
|
+
@aws.awscli.command_iam.getPolicy.execute(policy_arn, 'json', verbose)
|
276
|
+
version=@aws.awscli.data_iam.policy.defaultVersion
|
277
|
+
@aws.awscli.command_iam.getPolicyVersion.execute(policy_arn, version, 'json', verbose)
|
278
|
+
allowed_instanceids = @aws.awscli.data_iam.policy_document.resource_instance_ids()
|
279
|
+
@aws.awscli.command_ec2.describeInstances.execute(region, 'json', {}, textout, verbose)
|
280
|
+
target_instanceid = @aws.awscli.data_ec2.instance.instanceid(name, externalid)
|
281
|
+
if allowed_instanceids =~ /#{target_instanceid}/
|
282
|
+
now_time = overridebasetime ? overridebasetime.to_i : Time.now.to_i
|
283
|
+
interval_time = now_time + (hours.to_i*60*60)
|
284
|
+
tag_value="#{now_time}:#{interval_time}:#{email}"
|
285
|
+
@aws.awscli.command_ec2.createTags.execute(target_instanceid, region, 'interval', tag_value, textout, verbose)
|
286
|
+
textout.puts("Instance #{name ? name : externalid} tagged: Key=interval,Value=#{tag_value}") if textout
|
287
|
+
else
|
288
|
+
textout.puts("Target instance is not in the allowed list accoring to the specified policy.")
|
289
|
+
end
|
290
|
+
end
|
291
|
+
|
292
|
+
def interval_cron(policy_arn=nil, region, textout, verbose, overridebasetime)
|
293
|
+
@aws.awscli.command_iam.getPolicy.execute(policy_arn, 'json', verbose)
|
294
|
+
version=@aws.awscli.data_iam.policy.defaultVersion
|
295
|
+
@aws.awscli.command_iam.getPolicyVersion.execute(policy_arn, version, 'json', verbose)
|
296
|
+
allowed_instanceids = @aws.awscli.data_iam.policy_document.resource_instance_ids()
|
297
|
+
@aws.awscli.command_ec2.describeInstances.execute(region, 'json', {}, textout, verbose)
|
298
|
+
allowed_instanceids.split("\n").each do |id|
|
299
|
+
instance_name = @aws.awscli.data_ec2.instance.name(id)
|
300
|
+
instance_externalid = @aws.awscli.data_ec2.instance.name(id)
|
301
|
+
instance_status = @aws.awscli.data_ec2.instance.status(instance_name, instance_externalid)
|
302
|
+
if @aws.awscli.data_ec2.instance.has_interval?(id)
|
303
|
+
interval_start = @aws.awscli.data_ec2.instance.interval_start(id)
|
304
|
+
interval_end = @aws.awscli.data_ec2.instance.interval_end(id)
|
305
|
+
interval_email = @aws.awscli.data_ec2.instance.interval_email(id)
|
306
|
+
now_time = overridebasetime ? overridebasetime.to_i : Time.now.to_i
|
307
|
+
verbose.puts "DEBUG: instance_name=#{instance_name},instance_externalid=#{instance_externalid}" if verbose
|
308
|
+
verbose.puts "DEBUG: instance_status=#{instance_status},interval_email=#{interval_email}" if verbose
|
309
|
+
verbose.puts "DEBUG: interval_start=#{interval_start},interval_end=#{interval_end}" if verbose
|
310
|
+
if now_time > interval_end.to_i and instance_status == "running"
|
311
|
+
@aws.awscli.command_ec2.stopInstances.execute(id, region, textout, verbose)
|
312
|
+
textout.puts("Instance #{instance_name} stopped.") if textout and instance_name
|
313
|
+
end
|
314
|
+
if now_time < interval_end.to_i and instance_status == "stopped"
|
315
|
+
@aws.awscli.command_ec2.runInstances.execute(id, region, textout, verbose)
|
316
|
+
textout.puts("Instance #{instance_name} started.") if textout and instance_name
|
317
|
+
end
|
318
|
+
else
|
319
|
+
textout.puts("Instance #{instance_name} does not have an interval set.")
|
320
|
+
end
|
321
|
+
end
|
322
|
+
end
|
323
|
+
|
324
|
+
def start(name=nil, externalid=nil, region, textout, verbose, skip_running_check)
|
325
|
+
@aws.awscli.command_ec2.describeInstances.execute(region, 'json', {}, textout, verbose)
|
326
|
+
instance_status = @aws.awscli.data_ec2.instance.status(name, externalid)
|
327
|
+
instance_id = @aws.awscli.data_ec2.instance.instanceid(name, externalid)
|
328
|
+
externalid = @aws.awscli.data_ec2.instance.externalid(instance_id)
|
329
|
+
case instance_status
|
330
|
+
when "stopped"
|
331
|
+
@aws.awscli.command_ec2.runInstances.execute(instance_id, region, textout, verbose)
|
332
|
+
instance_running?(region, nil, externalid, 60, 5, verbose) if not skip_running_check
|
333
|
+
textout.puts("Instance #{name} started.") if textout and name
|
334
|
+
end
|
335
|
+
end
|
336
|
+
|
337
|
+
def stop(name=nil, externalid=nil, region, textout, verbose, skip_running_check)
|
338
|
+
@aws.awscli.command_ec2.describeInstances.execute(region, 'json', {}, textout, verbose)
|
339
|
+
instance_status = @aws.awscli.data_ec2.instance.status(name, externalid)
|
340
|
+
instance_id = @aws.awscli.data_ec2.instance.instanceid(name, externalid)
|
341
|
+
externalid = @aws.awscli.data_ec2.instance.externalid(instance_id)
|
342
|
+
case instance_status
|
343
|
+
when "running"
|
344
|
+
@aws.awscli.command_ec2.stopInstances.execute(instance_id, region, textout, verbose)
|
345
|
+
textout.puts("Instance #{name} stopped.") if textout and name
|
346
|
+
end
|
347
|
+
end
|
348
|
+
|
349
|
+
end
|
350
|
+
end
|
351
|
+
end
|
352
|
+
end
|