ansible_module 0.9.1 → 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +4 -0
  3. data/README.md +100 -21
  4. data/VERSION +1 -1
  5. data/lib/ansible_module.rb +17 -18
  6. metadata +17 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d6355d7da66ac0cb19bc372a9733fba816217b87
4
- data.tar.gz: 4f34188771a2276f2397609b3540cabcb79c55cd
3
+ metadata.gz: cf1b28c2d28267e752955d791b48e7b2a1f90789
4
+ data.tar.gz: fcc36bebf0600969dd4fbdc437a83bf38365c6ca
5
5
  SHA512:
6
- metadata.gz: ab78c4fb84571570587976d59dfbf3a3ad9f076d2b8fe61210ecf94ba6cf81b067118dd9dc88d6ff6c5e48d29cb29a8203fc564ec0f84a20d0c5c683ece8400a
7
- data.tar.gz: ed41b1780a6439bfff9a3fe88d0418b5d42887380814af2be2efa9ba11ef9c10270599843b6df554d90dc51a744852fb6e9a9fb0fad67cf785c26d9da59e58af
6
+ metadata.gz: acfff12953c0ae8c0841c0d7a74cfed39d86ba90f5b83657cc77c913e34f433aa7c652b8b9839c545d3c97500198513ab694a8ef5a04d94f0af0f888ee52eb9d
7
+ data.tar.gz: 645ebb33640f937a8ee3b3fc7df4ec4f3c2c33704527bd6d69da3699bee1ee6ce6cdb9b96b20e5741cde7609d8a506dbe9ac93acd46f344197dad1286bee154f
@@ -1,6 +1,10 @@
1
1
  CHANGELOG
2
2
  =========
3
3
 
4
+ ## 0.9.2 (2014-08-31)
5
+
6
+ * Add initial RSpec examples.
7
+
4
8
  ## 0.9.1 (2014-08-28)
5
9
 
6
10
  * Add private method `AnsibleModule#invalid_json` to improve the message on
data/README.md CHANGED
@@ -19,7 +19,7 @@ The following is a typical procedure on Ubuntu Server 14.04:
19
19
  ```
20
20
  $ sudo add-apt-repository -y ppa:brightbox/ruby-ng
21
21
  $ sudo apt-get update
22
- $ sudo apt-get -y install ruby2.1
22
+ $ sudo apt-get -y install ruby2.1 ruby2.1-dev
23
23
  $ sudo gem install ansible_module
24
24
  ```
25
25
 
@@ -37,6 +37,8 @@ The following is an example for Ubuntu Server 14.04:
37
37
  apt_repository: repo='ppa:brightbox/ruby-ng' state=present
38
38
  - name: Install ruby 2.1
39
39
  apt: name=ruby2.1 state=present
40
+ - name: Install ruby 2.1 headers
41
+ apt: name=ruby2.1-dev state=present
40
42
  - name: Install ansible_module gem
41
43
  gem: name=ansible_module user_install=false state=present
42
44
  ```
@@ -50,8 +52,10 @@ $ ansible-playbook -i hosts ruby_environment.yml
50
52
  In the above example, the `hosts` file is an [inventory](http://docs.ansible.com/intro_inventory.html) which lists up host names or IP addresses.
51
53
 
52
54
 
53
- Example (1)
54
- -----------
55
+ Example (1) -- Simple Calculation
56
+ ---------------------------------
57
+
58
+ ### Module
55
59
 
56
60
  Create a file named `calc` on the `library` directory as follows:
57
61
 
@@ -62,10 +66,9 @@ require 'ansible_module'
62
66
 
63
67
  class Calc < AnsibleModule
64
68
  attribute :x, Integer
65
- attribute :y, Integer, default: 100
69
+ attribute :y, Integer
66
70
 
67
- validates :x, presence: true, numericality: { only_integer: true }
68
- validates :y, numericality: { only_integer: true }
71
+ validates :x, :y, presence: true, numericality: { only_integer: true }
69
72
 
70
73
  def main
71
74
  sum = x + y
@@ -81,7 +84,16 @@ The values of attributes `x` and `y` are set during instantiation process by `An
81
84
 
82
85
  Note that you can validate them with `validates` class method derived from `ActiveModel`.
83
86
 
84
- Then, create a file named `calc.yml` as follows:
87
+ The class method `instance` returns a singleton instance of `Calc` class,
88
+ and its `run` method calls the `main` method if validations are successful.
89
+
90
+ So, the author of an Ansible module must implement the `main` method at least.
91
+
92
+
93
+ ### Playbook
94
+
95
+ Now, you can use the `calc` module in your playbook.
96
+ For example, create a file named `calc.yml` as follows:
85
97
 
86
98
  ```yaml
87
99
  - hosts: servers
@@ -89,19 +101,52 @@ Then, create a file named `calc.yml` as follows:
89
101
  - name: Make a calculation
90
102
  calc: x=50 y=50
91
103
  register: result
92
- - debug: >
93
- msg="sum = {{ result['sum'] }}"
104
+ - debug: msg="sum = {{ result['sum'] }}"
94
105
  ```
95
106
 
96
- And run the following command on your local host:
107
+ Then, run the following command on your local host:
97
108
 
98
109
  ```
99
110
  $ ansible-playbook -i hosts calc.yml
100
111
  ```
101
112
 
102
113
 
103
- Example (2)
104
- -----------
114
+ Example (2) -- MySQL 5.6 Replication Management
115
+ -----------------------------------------------
116
+
117
+ ### Prerequisites
118
+
119
+ Install MySQL 5.6 Server, MySQL 5.6 Client, MySQL 5.6 development files and `mysql2` gem.
120
+
121
+ The following is a typical procedure on Ubuntu Server 14.04:
122
+
123
+ ```
124
+ $ sudo add-apt-repository -y ppa:ondrej/mysql-5.6
125
+ $ sudo apt-get update
126
+ $ sudo apt-get -y install mysql-server-5.6 mysql-client-5.6 libmysqlclient-dev
127
+ $ sudo gem install mysql2
128
+ ```
129
+
130
+ You can also install them with the following playbook:
131
+
132
+ ```yaml
133
+ - hosts: servers
134
+ sudo: yes
135
+ tasks:
136
+ - name: Add ppa for mysql 5.6
137
+ apt_repository: repo='ppa:ondrej/mysql-5.6' state=present
138
+ - name: Install mysql server 5.6
139
+ apt: name=mysql-server-5.6 state=present
140
+ - name: Install mysql client 5.6
141
+ apt: name=mysql-client-5.6 state=present
142
+ - name: Install libmysqlclient-dev
143
+ apt: name=libmysqlclient-dev state=present
144
+ - name: Install mysql2 gem
145
+ gem: name=mysql2 user_install=false state=present
146
+ ```
147
+
148
+
149
+ ### Module
105
150
 
106
151
  Create a file named `mysql_change_master` on the `library` directory as follows:
107
152
 
@@ -109,6 +154,7 @@ Create a file named `mysql_change_master` on the `library` directory as follows:
109
154
  #!/usr/bin/ruby
110
155
 
111
156
  require 'ansible_module'
157
+ require 'mysql2'
112
158
 
113
159
  class MysqlChangeMaster < AnsibleModule
114
160
  attribute :host, String
@@ -117,28 +163,59 @@ class MysqlChangeMaster < AnsibleModule
117
163
  attribute :password, String
118
164
  attribute :mysql_root_password, String
119
165
 
166
+ validates :host, :user, :password, presence: true
120
167
  validates :port, inclusion: { in: 0..65535 }
168
+ validates :password, maximum: 32
121
169
 
122
170
  def main
171
+ done? && exit_json(changed: false)
172
+
123
173
  statement = %Q{
124
- STOP SLAVE;
125
174
  CHANGE MASTER TO
126
175
  MASTER_HOST='#{host}',
127
176
  MASTER_PORT=#{port},
128
177
  MASTER_USER='#{user}',
129
178
  MASTER_PASSWORD='#{password}',
130
- MASTER_AUTO_POSITION=1;
131
- START SLAVE;
179
+ MASTER_AUTO_POSITION=1
132
180
  }.squish
133
181
 
134
- command = %Q{
135
- /usr/bin/mysql -u root -p#{mysql_root_password} -e "#{statement}"
136
- }.squish
182
+ mysql_client.query('STOP SLAVE')
183
+ mysql_client.query(statement)
184
+ mysql_client.query('START SLAVE')
185
+ sleep(1)
137
186
 
138
- system(command)
139
-
140
- exit_json(statement: statement, changed: true)
187
+ if done?
188
+ exit_json(statement: statement, changed: true)
189
+ else
190
+ fail_json(msg: "Last Error: #{@last_error}")
191
+ end
141
192
  end
193
+
194
+ private
195
+
196
+ def done?
197
+ status = mysql_client.query('SHOW SLAVE STATUS').first || {}
198
+
199
+ @last_error = [ status['Last_IO_Error'], status['Last_SQL_Error'] ]
200
+ .compact.join(' ').squish
201
+
202
+ status['Master_Host'] == host &&
203
+ status['Master_User'] == user &&
204
+ status['Master_Port'].to_i == port &&
205
+ status['Auto_Position'].to_i == 1 &&
206
+ status['Slave_IO_State'] != '' &&
207
+ status['Last_IO_Error'] == '' &&
208
+ status['Last_SQL_Error'] == ''
209
+ end
210
+
211
+ def mysql_client
212
+ @client ||= Mysql2::Client.new(
213
+ host: 'localhost',
214
+ username: 'root',
215
+ password: mysql_root_password,
216
+ encoding: 'utf8'
217
+ )
218
+ end
142
219
  end
143
220
 
144
221
  MysqlChangeMaster.instance.run
@@ -146,6 +223,8 @@ MysqlChangeMaster.instance.run
146
223
 
147
224
  Note that you can use methods added by `ActiveSupport` like `String#squish`.
148
225
 
226
+ ### Playbook
227
+
149
228
  Then, create a file named `replication.yml` as follows:
150
229
 
151
230
  ```yaml
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.9.1
1
+ 0.9.2
@@ -24,30 +24,29 @@ class AnsibleModule
24
24
 
25
25
  private
26
26
 
27
- def exit_json(hash)
28
- hash = ActiveSupport::HashWithIndifferentAccess.new(hash)
29
- print JSON.dump(hash)
30
- exit 0
31
- end
27
+ def exit_json(hash)
28
+ hash = ActiveSupport::HashWithIndifferentAccess.new(hash)
29
+ print JSON.dump(hash)
30
+ exit 0
31
+ end
32
32
 
33
- def fail_json(hash)
34
- hash = ActiveSupport::HashWithIndifferentAccess.new(hash)
35
- hash[:failed] = true
36
- hash[:msg] ||= "No error message."
37
- print JSON.dump(hash)
38
- exit 1
39
- end
33
+ def fail_json(hash)
34
+ hash = ActiveSupport::HashWithIndifferentAccess.new(hash)
35
+ hash[:failed] = true
36
+ hash[:msg] ||= "No error message."
37
+ print JSON.dump(hash)
38
+ exit 1
39
+ end
40
40
 
41
- def invalid_json
42
- message = 'Invalid parameters: '
43
- message += errors.full_messages.map { |m| "#{m}." }.join(' ')
44
- fail_json(msg: message)
45
- end
41
+ def invalid_json
42
+ message = 'Invalid parameters: '
43
+ message += errors.full_messages.map { |m| "#{m}." }.join(' ')
44
+ fail_json(msg: message)
45
+ end
46
46
 
47
47
  class << self
48
48
  def instance
49
49
  @instance ||= new(params)
50
- @instance
51
50
  end
52
51
 
53
52
  def params
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ansible_module
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 0.9.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tsutomu KURODA
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-28 00:00:00.000000000 Z
11
+ date: 2014-08-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ~>
67
67
  - !ruby/object:Gem::Version
68
68
  version: 4.1.5
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: 3.0.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: 3.0.0
69
83
  description: AnsibleModule is a Ruby class that provides basic functionalities as
70
84
  an Ansible module.
71
85
  email: t-kuroda@oiax.jp
@@ -101,5 +115,5 @@ rubyforge_project:
101
115
  rubygems_version: 2.1.10
102
116
  signing_key:
103
117
  specification_version: 4
104
- summary: AnsibleModule class for Ruby language.
118
+ summary: A Ruby base class for Ansible module.
105
119
  test_files: []