| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | use 5.010; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | $| = 1; | ||
| 8 | |||
| 9 | use Getopt::Long qw(:config pass_through); | ||
| 10 | |||
| 11 | use Unicorn::Manager::CLI; | ||
| 12 | use IO::Socket; | ||
| 13 | use JSON; | ||
| 14 | |||
| 15 | my $HELP = <<"END"; | ||
| 16 | Synopsis | ||
| 17 | $0 [action] [options] | ||
| 18 | |||
| 19 | Actions | ||
| 20 | help | ||
| 21 | show this help | ||
| 22 | show | ||
| 23 | dumps a structure of user ids and the process ids of masters | ||
| 24 | and their children | ||
| 25 | start | ||
| 26 | starts a users unicorn server, requires --config to be specified | ||
| 27 | stop | ||
| 28 | stops unicorn | ||
| 29 | restart | ||
| 30 | gracefully restarts unicorn | ||
| 31 | reload | ||
| 32 | reload unicorn | ||
| 33 | add_worker | ||
| 34 | adds a unicorn worker | ||
| 35 | rm_worker | ||
| 36 | removes a unicorn worker | ||
| 37 | version | ||
| 38 | print Unicorn::Manager version | ||
| 39 | query | ||
| 40 | to be implemented | ||
| 41 | |||
| 42 | Options | ||
| 43 | -u, --user | ||
| 44 | username of unicorns owner (can be ommited if user is not root) | ||
| 45 | -c, --config | ||
| 46 | path to the unicorn config file | ||
| 47 | --args | ||
| 48 | optional additional arguments used with action 'start' | ||
| 49 | overrides options of the config file | ||
| 50 | see section Examples for proper usage | ||
| 51 | "-D" is set default | ||
| 52 | --debug | ||
| 53 | flag to enable debug output | ||
| 54 | --rails | ||
| 55 | defaults to 1 for now. so it has no effect at all | ||
| 56 | |||
| 57 | Examples | ||
| 58 | uc.pl show | ||
| 59 | uc.pl start -u railsuser -c /home/railsuser/app/unicorn.rb --args "--listen 0.0.0.0:80, -D" | ||
| 60 | uc.pl restart -u railsuser | ||
| 61 | uc.pl add_worker | ||
| 62 | |||
| 63 | END | ||
| 64 | |||
| 65 | my $user; | ||
| 66 | my $config; | ||
| 67 | my $host; | ||
| 68 | my $port = 4242; | ||
| 69 | my $args = undef; | ||
| 70 | my $DEBUG = 0; | ||
| 71 | my $rails = 1; | ||
| 72 | |||
| 73 | my $result = GetOptions( | ||
| 74 | 'user|u=s' => \$user, | ||
| 75 | 'config|c=s' => \$config, | ||
| 76 | 'args=s' => \$args, | ||
| 77 | 'debug' => \$DEBUG, | ||
| 78 | 'rails' => \$rails, | ||
| 79 | 'host|h=s' => \$host, | ||
| 80 | 'port|p=i' => \$port, | ||
| 81 | ); | ||
| 82 | |||
| 83 | my ( $action, @params ) = @ARGV; | ||
| 84 | |||
| 85 | if ( $> > 0 ) { | ||
| 86 | $user = getpwuid $> unless $user; | ||
| 87 | } | ||
| 88 | else { | ||
| 89 | $user = 'nobody' unless $user; | ||
| 90 | } | ||
| 91 | |||
| 92 | unless ( $user && $action ) { | ||
| 93 | print $HELP; | ||
| 94 | die "Missing arguments. username and action are required\n"; | ||
| 95 | } | ||
| 96 | |||
| 97 | my $arg_ref = []; | ||
| 98 | |||
| 99 | # make -D default as most of the time you will want to start Unicorn as daemon | ||
| 100 | $args = "-D" unless defined $args; | ||
| 101 | |||
| 102 | $arg_ref = [ split ',', $args ] if $args; | ||
| 103 | |||
| 104 | my $unicorn = sub { | ||
| 105 | return Unicorn::Manager::CLI->new( | ||
| 106 | username => $user, | ||
| 107 | rails => $rails, | ||
| 108 | DEBUG => $DEBUG, | ||
| 109 | ); | ||
| 110 | }; | ||
| 111 | |||
| 112 | my $dispatch_cli = { | ||
| 113 | help => sub { | ||
| 114 | say $HELP; | ||
| 115 | exit 0; | ||
| 116 | }, | ||
| 117 | show => sub { | ||
| 118 | my $uc = Unicorn::Manager::CLI->new( | ||
| 119 | username => 'nobody', | ||
| 120 | DEBUG => $DEBUG, | ||
| 121 | ); | ||
| 122 | |||
| 123 | my $uidref = $uc->proc->process_table->ptable; | ||
| 124 | |||
| 125 | for ( keys %{$uidref} ) { | ||
| 126 | my $username = getpwuid $_; | ||
| 127 | my $pidref = $uidref->{$_}; | ||
| 128 | |||
| 129 | print "$username:\n"; | ||
| 130 | |||
| 131 | for my $master ( keys %{$pidref} ) { | ||
| 132 | print " master: $master\n"; | ||
| 133 | for my $worker ( @{ $pidref->{$master} } ) { | ||
| 134 | if ( ref($worker) ~~ 'HASH' ) { | ||
| 135 | for ( keys %$worker ) { | ||
| 136 | print " new master: " . $_ . "\n"; | ||
| 137 | print " new worker: $_\n" for @{ $worker->{$_} }; | ||
| 138 | } | ||
| 139 | } | ||
| 140 | else { | ||
| 141 | print " worker: $worker\n"; | ||
| 142 | } | ||
| 143 | } | ||
| 144 | } | ||
| 145 | } | ||
| 146 | |||
| 147 | exit 0; | ||
| 148 | }, | ||
| 149 | start => sub { | ||
| 150 | unless ($config) { | ||
| 151 | print $HELP; | ||
| 152 | die "Action 'start' requires a config file.\n"; | ||
| 153 | } | ||
| 154 | if ($DEBUG) { | ||
| 155 | say "\$unicorn->start( config => \$config, args => \$arg_ref )"; | ||
| 156 | say " -> \$config => $config"; | ||
| 157 | use Data::Dumper; | ||
| 158 | say " -> \$arg_ref => " . Dumper($arg_ref); | ||
| 159 | } | ||
| 160 | return $unicorn->()->start( { config => $config, args => $arg_ref } ); | ||
| 161 | }, | ||
| 162 | stop => sub { | ||
| 163 | return $unicorn->()->stop; | ||
| 164 | }, | ||
| 165 | restart => sub { | ||
| 166 | return $unicorn->()->restart( { mode => 'graceful' } ); | ||
| 167 | }, | ||
| 168 | reload => sub { | ||
| 169 | return $unicorn->()->reload; | ||
| 170 | }, | ||
| 171 | add_worker => sub { | ||
| 172 | return $unicorn->()->add_worker( { num => 1 } ); | ||
| 173 | }, | ||
| 174 | rm_worker => sub { | ||
| 175 | return $unicorn->()->remove_worker( { num => 1 } ); | ||
| 176 | }, | ||
| 177 | version => sub { | ||
| 178 | return Unicorn::Manager::Version->get; | ||
| 179 | }, | ||
| 180 | query => sub { | ||
| 181 | $params[0] = 'help' unless @params; | ||
| 182 | return $unicorn->()->query(@params); | ||
| 183 | }, | ||
| 184 | }; | ||
| 185 | |||
| 186 | my $dispatch_server = { | ||
| 187 | query => sub { | ||
| 188 | my ( $query, @args ) = @params; | ||
| 189 | my $data = { | ||
| 190 | query => $query, | ||
| 191 | args => [@args], | ||
| 192 | }; | ||
| 193 | my $json = JSON->new->utf8(1); | ||
| 194 | |||
| 195 | my $sock = IO::Socket::INET->new( | ||
| 196 | PeerAddr => $host || 'localhost', | ||
| 197 | PeerPort => $port || 4242, | ||
| 198 | Proto => 'tcp', | ||
| 199 | ); | ||
| 200 | |||
| 201 | my $json_string = $json->encode($data); | ||
| 202 | my $res; | ||
| 203 | |||
| 204 | if ( not $sock ) { | ||
| 205 | say "Apparently the Unicorn::Manager::Server is not running or not accessible."; | ||
| 206 | say "Try running without the host command line switch or check your firewall."; | ||
| 207 | |||
| 208 | exit 1; | ||
| 209 | } | ||
| 210 | |||
| 211 | print $sock "$json_string\n"; | ||
| 212 | |||
| 213 | while (<$sock>) { | ||
| 214 | $res .= $_; | ||
| 215 | } | ||
| 216 | |||
| 217 | close $sock; | ||
| 218 | |||
| 219 | return $res; | ||
| 220 | }, | ||
| 221 | }; | ||
| 222 | |||
| 223 | my $response; | ||
| 224 | my $no_such_action = sub { | ||
| 225 | say "No action $action defined"; | ||
| 226 | exit 1; | ||
| 227 | }; | ||
| 228 | |||
| 229 | if ($host) { | ||
| 230 | if ( exists $dispatch_server->{$action} ) { | ||
| 231 | $response = $dispatch_server->{$action}->(); | ||
| 232 | } | ||
| 233 | else { | ||
| 234 | $no_such_action->(); | ||
| 235 | } | ||
| 236 | } | ||
| 237 | else { | ||
| 238 | if ( exists $dispatch_cli->{$action} ) { | ||
| 239 | $response = $dispatch_cli->{$action}->(); | ||
| 240 | } | ||
| 241 | else { | ||
| 242 | $no_such_action->(); | ||
| 243 | } | ||
| 244 | } | ||
| 245 | |||
| 246 | say $response; | ||
| 247 | |||
| 248 | exit 0; | ||
| 249 | |||
| 250 | __END__ |