rezept 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a084f39244aeebaf00fd8d2307de6c7dd189a4e5
4
- data.tar.gz: f6b24452efdbdf3798883f4be75515d6b57ee779
3
+ metadata.gz: 93ba080e87808028a6d1edb483253b8d3bc721e8
4
+ data.tar.gz: e725df836b7f0b506af99e7a466e94ce7884c20a
5
5
  SHA512:
6
- metadata.gz: ce615a416a985a938879af31ca0f6d6628c3b86f446a94bd5a0379066c6cad75506387b50ee54438a6f75b7a638515c6b6a40b80aa19ab549f24190fec4e570b
7
- data.tar.gz: 796d1a2eab98ee9ca2bf847126a67b416f5f4ca778f3e4c1ba589083555f07c82cb055109cbceff53023a877f312828d05e6b8b112c672901984b4aeadfba116
6
+ metadata.gz: c0bcb318a0130aaab5d6dd78ab73668eaf68dc81074d51ec3a9e50d8b6d7619f6583e362479561e257870cf07de9f3185f18dea62da7edd4a6f072bbfd33c085
7
+ data.tar.gz: 11bbdf453fa3b64696ad5febbb9cef60096737d3814cc0b0c2c9cc8202234891a921c421f28961b8f7923a5d759fab0bc88c7be4d3a84a513ae42dab989a2be8
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## 0.2.0
2
+ - Add `put_inventory` command [#2][]
3
+ - Add the options to search target instances from inventory to `run_command` [#2][]
4
+ - `--inventory/-I`
5
+ - `--conditions/-C`
6
+
1
7
  ## 0.1.0
2
8
 
3
9
  - Add `run_command` command [#1][]
@@ -10,4 +16,5 @@
10
16
  ## 0.0.1 (yunked)
11
17
 
12
18
  <!--- The following link definition list is generated by PimpMyChangelog --->
13
- [#1]: https://github.com/serverworks/rezept/issues/1
19
+ [#1]: https://github.com/serverworks/rezept/issues/1
20
+ [#2]: https://github.com/serverworks/rezept/issues/2
data/README.md CHANGED
@@ -109,12 +109,14 @@ Run the commands
109
109
  ```
110
110
  $ rezept help run_command
111
111
  Usage:
112
- rezept run_command -n, --name=NAME
112
+ rezept run_command -d, --document=DOCUMENT
113
113
 
114
114
  Options:
115
- -n, --name=NAME # Name of the document
115
+ -d, --document=DOCUMENT # The name of the document
116
116
  -i, [--instance-ids=one two three] # EC2 Instance IDs
117
117
  -t, [--tags=key:value] # EC2 Instance tags
118
+ -I, [--inventory=INVENTORY] # The name of the inventory type
119
+ -C, [--conditions=one two three] # The conditions to search inventories (ex. "Foo = Bar", "Buz > 1.0")
118
120
  -p, [--parameters=key:value] # Parameters for the document
119
121
  [--dry-run], [--no-dry-run] # Dry run (Only output the targets)
120
122
  [--wait], [--no-wait] # Wait and check for all results
@@ -130,6 +132,29 @@ Options:
130
132
  - If you specify multiple values to `tags` and `parameters`, separate them with commas(`,`).
131
133
  - When you use the `wait` option, the exit code will be `0` if the commands succeed on the all instances, else it will be `1`.
132
134
 
135
+ #### put_inventory
136
+ Put the inventory
137
+
138
+ ```
139
+ $ rezept help put_inventory
140
+ Usage:
141
+ rezept put_inventory -c, --content=key:value -i, --instance-id=INSTANCE_ID -n, --name=NAME
142
+
143
+ Options:
144
+ -n, --name=NAME # The name of the inventory type
145
+ -i, --instance-id=INSTANCE_ID # EC2 Instance ID
146
+ -c, --content=key:value # Parameters for the document
147
+ [--schema-version=SCHEMA_VERSION] # The schema version for the inventory item
148
+ # Default: 1.0
149
+ -f, [--file=FILE] # Configuration file
150
+ # Default: Docfile
151
+ [--color], [--no-color] # Disable colorize
152
+ # Default: true
153
+ [--amazon-docs], [--no-amazon-docs] # Include Amazon owned documents
154
+ [--dsl-content], [--no-dsl-content] # Convert JSON contents to DSL
155
+ # Default: true
156
+ ```
157
+
133
158
  ## Advanced methods
134
159
 
135
160
  #### Script styled commands (__script)
@@ -50,18 +50,18 @@ module Rezept
50
50
  fmt = 'ruby'
51
51
  end
52
52
 
53
- info("Document: '#{options['name']}'")
53
+ info("Document: '#{options['document']}'")
54
54
  info("Document Type: '#{options['type']}'")
55
55
 
56
56
  case fmt
57
57
  when 'json'
58
58
  docs = @converter.dslfile_to_h(options['file'])
59
- docs = docs.select {|d| d['name'] == options['name'] }
59
+ docs = docs.select {|d| d['name'] == options['document'] }
60
60
  ret = JSON.pretty_generate(JSON.parse(docs[0]['content']))
61
61
  Rezept::Utils.print_json(ret)
62
62
  when 'ruby'
63
63
  doc = {}
64
- doc['name'] = options['name']
64
+ doc['name'] = options['document']
65
65
  doc['document_type'] = options['type']
66
66
  doc['content'] = File.read(options['file'])
67
67
  ret = @converter.to_dsl(doc)
@@ -75,15 +75,64 @@ module Rezept
75
75
  def run_command(options)
76
76
  dry_run = options['dry_run'] ? '[Dry run] ' : ''
77
77
 
78
- if options['instance_ids'].nil? and options['tags'].nil?
79
- raise "Please specify the targets (--instance-ids/-i' or '--target-tags/-t')"
78
+ if options['instance_ids'].nil? and options['tags'].nil? and (options['inventory'].nil? or options['conditions'].nil?)
79
+ raise "Please specify the targets (--instance-ids/-i' or '--target-tags/-t' or '--inventroty/-I and --conditions/-C')"
80
80
  end
81
81
 
82
82
  instances = @client.get_target_instances(
83
83
  options['instance_ids'],
84
84
  _tags_to_criteria(options['tags'], 'name')
85
85
  )
86
+
86
87
  info("#{dry_run}Target instances...")
88
+
89
+ unless options['inventory'].nil?
90
+ instances = _filter_by_inventory(instances, options['inventory'], options['conditions'])
91
+ raise "Can't find target instances from inventories" if instances.empty?
92
+ end
93
+ _print_instances(instances)
94
+
95
+ instance_ids = options['instance_ids']
96
+ if instance_ids.nil? and not options['inventory'].nil?
97
+ instance_ids = []
98
+ instances.each {|i| instance_ids << i.instance_id}
99
+ end
100
+
101
+ if dry_run.empty?
102
+ command = @client.run_command(
103
+ options['document'],
104
+ instance_ids,
105
+ _tags_to_criteria(options['tags'], 'key'),
106
+ _convert_paraeters(options['parameters'])
107
+ )
108
+ _wait_all_results(command.command_id) if options['wait']
109
+ end
110
+ end
111
+
112
+ def _filter_by_inventory(instances, inventory, conditions)
113
+ filters = _conditions_to_filters(conditions)
114
+ ret = []
115
+ instances.each do |i|
116
+ inventory = @client.list_inventory_entries(
117
+ i.instance_id,
118
+ inventory,
119
+ filters,
120
+ )
121
+ ret << i unless inventory.entries.empty?
122
+ end
123
+ ret
124
+ end
125
+
126
+ def put_inventory(options)
127
+ @client.put_inventory(
128
+ options['instance_id'],
129
+ options['name'],
130
+ options['schema_version'],
131
+ options['content']
132
+ )
133
+ end
134
+
135
+ def _print_instances(instances)
87
136
  instances.each do |instance|
88
137
  name_tag = instance.tags.select {|i| i.key == 'Name'}
89
138
  if name_tag.empty?
@@ -92,16 +141,23 @@ module Rezept
92
141
  info("- #{name_tag[0].value} (#{instance.instance_id})")
93
142
  end
94
143
  end
144
+ end
95
145
 
96
- if dry_run.empty?
97
- command = @client.run_command(
98
- options['name'],
99
- options['instance_ids'],
100
- _tags_to_criteria(options['tags'], 'key'),
101
- _convert_paraeters(options['parameters'])
102
- )
103
- _wait_all_results(command.command_id) if options['wait']
146
+ def _conditions_to_filters(conditions)
147
+ ret = []
148
+ cond_simbols = {
149
+ '=' => 'Equal',
150
+ '!=' => 'NotEqual',
151
+ '<' => 'LessThan',
152
+ '>' => 'GreaterThan',
153
+ }
154
+ regexp = /^(?<key>[^=!<>\s]+)\s*(?<type>[=!<>]+)+\s*(?<value>.+)$/
155
+
156
+ conditions.each do |c|
157
+ m = regexp.match(c)
158
+ ret << {key: m[:key], values: m[:value].split(','), type: cond_simbols[m[:type]]}
104
159
  end
160
+ ret
105
161
  end
106
162
 
107
163
  def _tags_to_criteria(targets, key_name)
data/lib/rezept/cli.rb CHANGED
@@ -29,7 +29,7 @@ module Rezept
29
29
  end
30
30
 
31
31
  desc "convert", "Convert the documents to the other format"
32
- option :name, aliases: '-n', desc: 'Name of the document', type: :string, required: true
32
+ option :document, aliases: '-d', desc: 'The name of the document', type: :string, required: true
33
33
  option :type, aliases: '-t', desc: 'Type of the document (Command|Automation)', type: :string, required: true
34
34
  option :format, desc: 'Output format (json|ruby)', type: :string
35
35
  option :output, aliases: '-o', desc: 'Output filename (path)', type: :string
@@ -38,14 +38,25 @@ module Rezept
38
38
  end
39
39
 
40
40
  desc "run_command", "Run the commands"
41
- option :name, aliases: '-n', desc: 'Name of the document', type: :string, required: true
41
+ option :document, aliases: '-d', desc: 'The name of the document', type: :string, required: true
42
42
  option :instance_ids, aliases: '-i', desc: 'EC2 Instance IDs', type: :array
43
43
  option :tags, aliases: '-t', desc: 'EC2 Instance tags', type: :hash
44
+ option :inventory, aliases: '-I', desc: 'The name of the inventory type', type: :string
45
+ option :conditions, aliases: '-C', desc: 'The conditions to search inventories (ex. "Foo = Bar", "Buz > 1.0")', type: :array
44
46
  option :parameters, aliases: '-p', desc: 'Parameters for the document', type: :hash
45
47
  option :dry_run, desc: 'Dry run (Only output the targets)', type: :boolean, default: false
46
48
  option :wait, desc: 'Wait and check for all results', type: :boolean, default: false
47
49
  def run_command
48
50
  @actions.run_command(options)
49
51
  end
52
+
53
+ desc "put_inventory", "Put the inventory"
54
+ option :name, aliases: '-n', desc: 'The name of the inventory type', type: :string, required: true
55
+ option :instance_id, aliases: '-i', desc: 'EC2 Instance ID', type: :string, required: true
56
+ option :content, aliases: '-c', desc: 'Parameters for the document', type: :hash, required: true
57
+ option :schema_version, desc: 'The schema version for the inventory item', type: :string, default: '1.0'
58
+ def put_inventory
59
+ @actions.put_inventory(options)
60
+ end
50
61
  end
51
62
  end
data/lib/rezept/client.rb CHANGED
@@ -62,7 +62,7 @@ module Rezept
62
62
  )
63
63
  end
64
64
 
65
- def get_target_instances(instance_ids, filters, next_token=nil)
65
+ def get_target_instances(instance_ids=nil, filters=nil, next_token=nil)
66
66
  instances = []
67
67
 
68
68
  ret = @ec2.describe_instances(
@@ -94,5 +94,27 @@ module Rezept
94
94
  invocations
95
95
  end
96
96
 
97
+ def put_inventory(instance_id, type_name, schema_version, content)
98
+ @ssm.put_inventory(
99
+ instance_id: instance_id,
100
+ items: [
101
+ {
102
+ type_name: type_name,
103
+ schema_version: schema_version,
104
+ capture_time: Time.now.strftime('%Y-%m-%dT%H:%M:%SZ'),
105
+ content: [content],
106
+ },
107
+ ],
108
+ )
109
+ end
110
+
111
+ def list_inventory_entries(instance_id, type_name, filters)
112
+ @ssm.list_inventory_entries(
113
+ instance_id: instance_id,
114
+ type_name: type_name,
115
+ filters: filters,
116
+ )
117
+ end
118
+
97
119
  end
98
120
  end
@@ -1,3 +1,3 @@
1
1
  module Rezept
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rezept
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Serverworks Co.,Ltd.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-02-21 00:00:00.000000000 Z
11
+ date: 2017-03-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk