line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Dancer2::Plugin::Etcd; |
2
|
2
|
|
|
2
|
|
105854
|
use utf8; |
|
2
|
|
|
|
|
24
|
|
|
2
|
|
|
|
|
10
|
|
3
|
2
|
|
|
2
|
|
48
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
36
|
|
4
|
2
|
|
|
2
|
|
8
|
use warnings; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
77
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
=encoding utf8 |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 NAME |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
Dancer2::Plugin::Etcd |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=cut |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION = '0.011'; |
15
|
|
|
|
|
|
|
|
16
|
2
|
|
|
2
|
|
700
|
use Dancer2::Core::Types qw/Bool HashRef Str/; |
|
2
|
|
|
|
|
364117
|
|
|
2
|
|
|
|
|
17
|
|
17
|
2
|
|
|
2
|
|
5061
|
use Dancer2::Plugin; |
|
2
|
|
|
|
|
110281
|
|
|
2
|
|
|
|
|
19
|
|
18
|
2
|
|
|
2
|
|
75204
|
use Data::Dumper; |
|
2
|
|
|
|
|
10548
|
|
|
2
|
|
|
|
|
124
|
|
19
|
2
|
|
|
2
|
|
17
|
use File::Spec; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
36
|
|
20
|
2
|
|
|
2
|
|
708
|
use Net::Etcd; |
|
2
|
|
|
|
|
297962
|
|
|
2
|
|
|
|
|
699
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=head1 SYNOPSIS |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
get '/foo/:name' => sub { |
25
|
|
|
|
|
|
|
$tokens = shift; |
26
|
|
|
|
|
|
|
$etcd = etcd('foo'); |
27
|
|
|
|
|
|
|
$name = param('name'); |
28
|
|
|
|
|
|
|
$tokens->{name} = $etcd->range({ key => $name })->get_value; |
29
|
|
|
|
|
|
|
[ ... ] |
30
|
|
|
|
|
|
|
}; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# save development configs to etcd |
33
|
|
|
|
|
|
|
shepherd put |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# retrieve latest version of configs |
36
|
|
|
|
|
|
|
shepherd get |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=head1 DESCRIPTION |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
The C keyword which is exported by this plugin allows you to |
41
|
|
|
|
|
|
|
connect directly to etcd v3 api. |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=head1 shepherd |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
By far the most interesting part of this module is shepherd. shepherd |
46
|
|
|
|
|
|
|
allows you to save Dancer App YAML configs to etcd by line as key/value. |
47
|
|
|
|
|
|
|
Even more interesting is that it maintains your comments. An example of |
48
|
|
|
|
|
|
|
usage would be spawning a container with the application then simply running |
49
|
|
|
|
|
|
|
shepherd get --env=production would bring in the latest production configs for |
50
|
|
|
|
|
|
|
your app. |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=head1 CONFIGURATION |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
single e.g.: |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
plugins: |
57
|
|
|
|
|
|
|
Etcd: |
58
|
|
|
|
|
|
|
host: 127.0.0.1 |
59
|
|
|
|
|
|
|
port: 4001 |
60
|
|
|
|
|
|
|
user: samb |
61
|
|
|
|
|
|
|
password: h3xFu5ion |
62
|
|
|
|
|
|
|
ssl: 1 |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
named e.g.: |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
plugins: |
67
|
|
|
|
|
|
|
Etcd: |
68
|
|
|
|
|
|
|
connections: |
69
|
|
|
|
|
|
|
foo: |
70
|
|
|
|
|
|
|
host: 127.0.0.1 |
71
|
|
|
|
|
|
|
port: 4001 |
72
|
|
|
|
|
|
|
user: samb |
73
|
|
|
|
|
|
|
password: h3xFu5ion |
74
|
|
|
|
|
|
|
bar: |
75
|
|
|
|
|
|
|
host: 127.0.0.1 |
76
|
|
|
|
|
|
|
port: 2379 |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=cut |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head2 connections |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=cut |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
has connections => ( |
85
|
|
|
|
|
|
|
is => 'ro', |
86
|
|
|
|
|
|
|
isa => HashRef, |
87
|
|
|
|
|
|
|
from_config => 'connections', |
88
|
|
|
|
|
|
|
default => sub { +{} }, |
89
|
|
|
|
|
|
|
); |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head2 host |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
default 127.0.0.1 |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=cut |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
has host => ( |
98
|
|
|
|
|
|
|
is => 'ro', |
99
|
|
|
|
|
|
|
isa => Str, |
100
|
|
|
|
|
|
|
default => sub { |
101
|
|
|
|
|
|
|
$_[0]->config->{host} || '127.0.0.1' |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
); |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head2 port |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
default 2379 |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=cut |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
has port => ( |
112
|
|
|
|
|
|
|
is => 'ro', |
113
|
|
|
|
|
|
|
default => sub { |
114
|
|
|
|
|
|
|
$_[0]->config->{port} || '2379' |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
); |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head2 etcd_connection_name |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=cut |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
has etcd_connection_name => ( |
123
|
|
|
|
|
|
|
is => 'ro', |
124
|
|
|
|
|
|
|
isa => Str, |
125
|
|
|
|
|
|
|
); |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=head2 username |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
Etcd username. |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=cut |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
has username => ( |
134
|
|
|
|
|
|
|
is => 'ro', |
135
|
|
|
|
|
|
|
default => sub { |
136
|
|
|
|
|
|
|
$_[0]->config->{user} |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
); |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head2 password |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
Etcd user password. |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=cut |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
has password => ( |
148
|
|
|
|
|
|
|
is => 'ro', |
149
|
|
|
|
|
|
|
default => sub { |
150
|
|
|
|
|
|
|
$_[0]->config->{password} |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
); |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head2 ssl |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
Secure connection is recommended, |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=cut |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
has ssl => ( |
161
|
|
|
|
|
|
|
is => 'ro', |
162
|
|
|
|
|
|
|
default => sub { |
163
|
|
|
|
|
|
|
$_[0]->config->{ssl} |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
); |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
plugin_keywords 'etcd'; |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=head1 KEYWORDS |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=head2 etcd |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=cut |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
sub etcd { |
176
|
0
|
|
|
0
|
1
|
|
my( $plugin, $handle ) = @_; |
177
|
0
|
|
|
|
|
|
$plugin->app->log( "debug", "etcd handle: ", $handle ); |
178
|
|
|
|
|
|
|
# print STDERR Dumper($handle); |
179
|
0
|
|
|
|
|
|
my $host; |
180
|
0
|
|
0
|
|
|
|
$handle ||= ''; |
181
|
0
|
|
|
|
|
|
my $settings; |
182
|
0
|
0
|
|
|
|
|
if ($handle) { |
183
|
|
|
|
|
|
|
# print STDERR Dumper($plugin->connections); |
184
|
0
|
0
|
|
|
|
|
$settings = $plugin->connections->{$handle} or return; |
185
|
0
|
|
|
|
|
|
$host = $settings->{host}; |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
else { |
188
|
0
|
|
|
|
|
|
for (qw(username password port)) { |
189
|
0
|
|
|
|
|
|
$settings->{$_} = $plugin->{$_}; |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
} |
192
|
0
|
|
|
|
|
|
return Net::Etcd->new( $settings ); |
193
|
|
|
|
|
|
|
}; |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=head1 AUTHOR |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
Sam Batschelet, |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
The L developers and community for their great application framework |
202
|
|
|
|
|
|
|
and for their quick and competent support. |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
Copyright 2016 Sam Batschelet (hexfusion). |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
209
|
|
|
|
|
|
|
under the terms of either: the GNU General Public License as published |
210
|
|
|
|
|
|
|
by the Free Software Foundation; or the Artistic License. |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
See http://dev.perl.org/licenses/ for more information. |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=cut |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
1; |