podnix 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,166 +0,0 @@
1
- # Copyright:: Copyright (c) 2012, 2013 Megam Systems
2
- # License:: Apache License, Version 2.0
3
- #
4
- # Licensed under the Apache License, Version 2.0 (the "License");
5
- # you may not use this file except in compliance with the License.
6
- # You may obtain a copy of the License at
7
- #
8
- # http://www.apache.org/licenses/LICENSE-2.0
9
- #
10
- # Unless required by applicable law or agreed to in writing, software
11
- # distributed under the License is distributed on an "AS IS" BASIS,
12
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- # See the License for the specific language governing permissions and
14
- # limitations under the License.
15
- #
16
- module Podnix
17
- class NodeCollection
18
- include Enumerable
19
-
20
-
21
- attr_reader :iterator
22
- def initialize
23
- @nodes = Array.new
24
- @nodes_by_name = Hash.new
25
- @insert_after_idx = nil
26
- end
27
-
28
- def all_nodes
29
- @nodes
30
- end
31
-
32
- def [](index)
33
- @nodes[index]
34
- end
35
-
36
- def []=(index, arg)
37
- is_megam_node(arg)
38
- @nodes[index] = arg
39
- @nodes_by_name[arg.node_name] = index
40
- end
41
-
42
- def <<(*args)
43
- args.flatten.each do |a|
44
- is_megam_node(a)
45
- @nodes << a
46
- @nodes_by_name[a.node_name] = @nodes.length - 1
47
- end
48
- self
49
- end
50
-
51
- # 'push' is an alias method to <<
52
- alias_method :push, :<<
53
-
54
- def insert(node)
55
- is_megam_node(node)
56
- if @insert_after_idx
57
- # in the middle of executing a run, so any nodes inserted now should
58
- # be placed after the most recent addition done by the currently executing
59
- # node
60
- @nodes.insert(@insert_after_idx + 1, node)
61
- # update name -> location mappings and register new node
62
- @nodes_by_name.each_key do |key|
63
- @nodes_by_name[key] += 1 if @nodes_by_name[key] > @insert_after_idx
64
- end
65
- @nodes_by_name[node.node_name] = @insert_after_idx + 1
66
- @insert_after_idx += 1
67
- else
68
- @nodes << node
69
- @nodes_by_name[node.node_name] = @nodes.length - 1
70
- end
71
- end
72
-
73
- def each
74
- @nodes.each do |node|
75
- yield node
76
- end
77
- end
78
-
79
- def each_index
80
- @nodes.each_index do |i|
81
- yield i
82
- end
83
- end
84
-
85
- def empty?
86
- @nodes.empty?
87
- end
88
-
89
- def lookup(node)
90
- lookup_by = nil
91
- if node.kind_of?(Megam::Node)
92
- lookup_by = node.node_name
93
- elsif node.kind_of?(String)
94
- lookup_by = node
95
- else
96
- raise ArgumentError, "Must pass a Megam::Node or String to lookup"
97
- end
98
- res = @nodes_by_name[lookup_by]
99
- unless res
100
- raise ArgumentError, "Cannot find a node matching #{lookup_by} (did you define it first?)"
101
- end
102
- @nodes[res]
103
- end
104
-
105
- # Transform the ruby obj -> to a Hash
106
- def to_hash
107
- index_hash = Hash.new
108
- self.each do |node|
109
- index_hash[node.node_name] = node.to_s
110
- end
111
- index_hash
112
- end
113
-
114
- # Serialize this object as a hash: called from JsonCompat.
115
- # Verify if this called from JsonCompat during testing.
116
- def to_json(*a)
117
- for_json.to_json(*a)
118
- end
119
-
120
-
121
- =begin
122
- ["json_claz":"Megam::NodeCollection",{
123
- "id":"NOD362428315933343744",
124
- "accounts_id":"ACT362211963352121344",
125
- "json_claz":"Megam::Node",
126
- "request":{
127
- "req_id":"NOD362428315933343744",
128
- "command":"commands"
129
- },
130
- "predefs":{
131
- "name":"",
132
- "scm":"",
133
- "war":"",
134
- "db":"",
135
- "queue":""
136
- }
137
- }]
138
- =end
139
- def self.json_create(o)
140
- collection = self.new()
141
- o["results"].each do |nodes_list|
142
- nodes_array = nodes_list.kind_of?(Array) ? nodes_list : [ nodes_list ]
143
- nodes_array.each do |node|
144
- collection.insert(node)
145
- end
146
- end
147
- collection
148
- end
149
-
150
- private
151
-
152
-
153
-
154
- def is_megam_node(arg)
155
- unless arg.kind_of?(Megam::Node)
156
- raise ArgumentError, "Members must be Megam::Node's"
157
- end
158
- true
159
- end
160
-
161
- def to_s
162
- Podnix::Stuff.styled_hash(to_hash)
163
- end
164
-
165
- end
166
- end
@@ -1,69 +0,0 @@
1
- #
2
- # License:: Apache License, Version 2.0
3
- #
4
- # Licensed under the Apache License, Version 2.0 (the "License");
5
- # you may not use this file except in compliance with the License.
6
- # You may obtain a copy of the License at
7
- #
8
- # http://www.apache.org/licenses/LICENSE-2.0
9
- #
10
- # Unless required by applicable law or agreed to in writing, software
11
- # distributed under the License is distributed on an "AS IS" BASIS,
12
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- # See the License for the specific language governing permissions and
14
- # limitations under the License.
15
-
16
- module Podnix
17
- module Stuff
18
- extend self
19
- def has_git?
20
- %x{ git --version }
21
- $?.success?
22
- end
23
-
24
- def git(args)
25
- return "" unless has_git?
26
- flattened_args = [args].flatten.compact.join(" ")
27
- %x{ git #{flattened_args} 2>&1 }.strip
28
- end
29
-
30
- def time_ago(since)
31
- if since.is_a?(String)
32
- since = Time.parse(since)
33
- end
34
-
35
- elapsed = Time.now - since
36
-
37
- message = since.strftime("%Y/%m/%d %H:%M:%S")
38
- if elapsed <= 60
39
- message << " (~ #{elapsed.floor}s ago)"
40
- elsif elapsed <= (60 * 60)
41
- message << " (~ #{(elapsed / 60).floor}m ago)"
42
- elsif elapsed <= (60 * 60 * 25)
43
- message << " (~ #{(elapsed / 60 / 60).floor}h ago)"
44
- end
45
- message
46
- end
47
-
48
- def spinner(ticks)
49
- %w(/ - \\ |)[ticks % 4]
50
- end
51
-
52
- def launchy(message, url)
53
- action(message) do
54
- require("launchy")
55
- launchy = Launchy.open(url)
56
- if launchy.respond_to?(:join)
57
- launchy.join
58
- end
59
- end
60
- end
61
-
62
- #
63
- #left justified keyed hash with newlines.
64
- def styled_hash(hash)
65
- hash.map{|k,v| "#{k.ljust(15)}=#{v}"}.join("\n")
66
- end
67
-
68
- end
69
- end