houcho 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,155 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
- class RoleHandle
3
- require 'yaml'
4
- require 'tempfile'
5
- require 'find'
6
- end
7
-
8
- class RoleHandle
9
- class YamlLoader
10
- attr_reader :data
11
-
12
- def initialize(yaml_file)
13
- @data = YAML.load_file(yaml_file)
14
- @data ||= {}
15
- end
16
- end
17
-
18
- class YamlEditor < YamlLoader
19
- attr_accessor :data
20
-
21
- def initialize(yaml_file)
22
- super
23
- @yaml_file = yaml_file
24
- end
25
-
26
- def save_to_file
27
- open(@yaml_file, 'w') do |f|
28
- YAML.dump(@data, f)
29
- end
30
- end
31
- end
32
-
33
- class ElementHandler
34
- def initialize(yaml)
35
- @elements = YamlEditor.new(yaml)
36
- end
37
-
38
- def elements(index = nil)
39
- if index
40
- (@elements.data[index]||[]).uniq
41
- else
42
- @elements.data.values.flatten.uniq
43
- end
44
- end
45
-
46
- def attach(index, element)
47
- @elements.data[index] ||= []
48
- @elements.data[index] << element
49
- @elements.save_to_file
50
- end
51
-
52
- def detach(index, element)
53
- @elements.data[index].delete(element)
54
- @elements.save_to_file
55
- end
56
-
57
- def attached?(index, element)
58
- return false if ! @elements.data.has_key?(index)
59
- @elements.data[index].include?(element)
60
- end
61
-
62
- # roleを消そうとしているオブジェクトからの問い合わせ用
63
- def has_data?(index)
64
- return false if ! @elements.data.has_key?(index)
65
- @elements.data[index].size != 0
66
- end
67
-
68
- def indexes(element)
69
- return [] if ! @elements.data.values.flatten.include?(element)
70
- @elements.data.select {|index, elems|elems.include?(element)}.keys
71
- end
72
- end
73
-
74
- class RoleHandler
75
- def initialize(yaml)
76
- @roles = YamlEditor.new(yaml)
77
- end
78
-
79
- def create(role)
80
- @roles.data[(@roles.data.keys.max||0) + 1] = role
81
- @roles.save_to_file
82
- end
83
-
84
- def delete(id)
85
- @roles.data.delete(id)
86
- @roles.save_to_file
87
- end
88
-
89
- def rename(id, name)
90
- @roles.data[id] = name
91
- @roles.save_to_file
92
- end
93
-
94
- def index(role)
95
- @roles.data.invert[role]
96
- end
97
-
98
- def indexes_regexp(role)
99
- @roles.data.select {|index, rolename| rolename =~ role }.keys
100
- end
101
-
102
- def name(id)
103
- @roles.data[id]
104
- end
105
- end
106
-
107
- class CfLoader
108
- attr_reader :role_hosts
109
-
110
- def initialize(yaml_file)
111
- @role_hosts = {}
112
- elements = {}
113
- group = []
114
- File.open(yaml_file) do |f|
115
- f.each do |l|
116
- if l =~ /^---/
117
- if l =~ /^---\s+#(.+)$/
118
- group << $1.gsub(/\s/, '_')
119
- else
120
- group << 'NOGROUPNAME'
121
- end
122
- end
123
- end
124
- end
125
- File.open(yaml_file) do |f|
126
- i=0
127
- YAML.load_documents(f) do |data|
128
- elements[group[i]] ||= []
129
- elements[group[i]].concat data['servers']
130
- i+=1
131
- end
132
- end
133
-
134
- elements.each do |groupname, data|
135
- current_label = 'NOCATEGORYNAME'
136
-
137
- data.each do |d|
138
- if ! d['label'].nil?
139
- label = d['label'].gsub(/\s/, '_')
140
- current_label = label if current_label != label
141
- end
142
-
143
- d['hosts'].map! do |host|
144
- host = host.split(' ')
145
- host = host.size == 1 ? host[0] : host[1]
146
- end
147
-
148
- r = groupname + '::' + current_label + '::' + d['config'].sub(/\.yaml$/, '')
149
- ary = (@role_hosts[r] || []) | d['hosts']
150
- @role_hosts[r] = ary.uniq
151
- end
152
- end
153
- end
154
- end
155
- end