| 1 | package Sparrow::Commands::RemoteTask; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | use base 'Exporter'; | ||
| 6 | |||
| 7 | use Sparrow::Constants; | ||
| 8 | use Sparrow::Misc; | ||
| 9 | use Sparrow::Commands::Plugin; | ||
| 10 | use Sparrow::Commands::Project; | ||
| 11 | use Sparrow::Commands::Task; | ||
| 12 | |||
| 13 | use Carp; | ||
| 14 | use File::Basename; | ||
| 15 | use File::Path; | ||
| 16 | |||
| 17 | use JSON; | ||
| 18 | use Data::Dumper; | ||
| 19 | use File::Copy; | ||
| 20 | |||
| 21 | use Term::ANSIColor; | ||
| 22 | |||
| 23 | our @EXPORT = qw{ | ||
| 24 | |||
| 25 | remote_task_upload | ||
| 26 | remote_task_install | ||
| 27 | remote_task_run | ||
| 28 | remote_task_share | ||
| 29 | remote_task_hide | ||
| 30 | remote_task_list | ||
| 31 | remote_task_public_list | ||
| 32 | remote_task_remove | ||
| 33 | |||
| 34 | }; | ||
| 35 | |||
| 36 | |||
| 37 | sub remote_task_upload { | ||
| 38 | |||
| 39 | my $path = shift or confess "usage: remote_task_upload(*path,comment)"; | ||
| 40 | my $comment = shift; | ||
| 41 | |||
| 42 | my ($project, $task) = split '/', $path; | ||
| 43 | |||
| 44 | confess "unknown project" unless -d sparrow_root."/projects/$project"; | ||
| 45 | confess "unknown task" unless -d sparrow_root."/projects/$project/tasks/$task"; | ||
| 46 | |||
| 47 | my $task_set = task_get($project,$task); | ||
| 48 | |||
| 49 | confess "plugin not set" unless $task_set->{'plugin'}; | ||
| 50 | |||
| 51 | my $plugin_name = $task_set->{plugin}; | ||
| 52 | |||
| 53 | my $pdir = sparrow_root."/plugins/".($task_set->{'install_dir'}); | ||
| 54 | |||
| 55 | confess 'plugin not installed' unless -d $pdir; | ||
| 56 | |||
| 57 | my $plugin_name = $task_set->{plugin}; | ||
| 58 | s{.*@}[] for $plugin_name; # FIXME: only public plugins could be uploaded as remote tasks | ||
| 59 | |||
| 60 | my $cred; | ||
| 61 | |||
| 62 | if ($ENV{sph_user} and $ENV{sph_token}){ | ||
| 63 | $cred->{user} = $ENV{sph_user}; | ||
| 64 | $cred->{token} = $ENV{sph_token}; | ||
| 65 | } else { | ||
| 66 | # or read from $ENV{HOME}/sparrowhub.json | ||
| 67 | open F, "$ENV{HOME}/sparrowhub.json" or confess "can't open $ENV{HOME}/sparrowhub.json to read: $!"; | ||
| 68 | my $s = join "", <F>; | ||
| 69 | close F; | ||
| 70 | $cred = decode_json($s); | ||
| 71 | } | ||
| 72 | |||
| 73 | my $suite_cfg_old_path = sparrow_root."/projects/$project/tasks/$task/suite.ini"; | ||
| 74 | my $suite_cfg_path = sparrow_root."/projects/$project/tasks/$task/suite.cfg"; | ||
| 75 | |||
| 76 | $suite_cfg_path = $suite_cfg_old_path unless -f $suite_cfg_path; | ||
| 77 | |||
| 78 | execute_shell_command( | ||
| 79 | "curl -f -H 'sparrow-user: $cred->{user}' " . | ||
| 80 | "-H 'sparrow-token: $cred->{token}' " .sparrow_hub_api_url().'/api/v1/remote-task/upload '. | ||
| 81 | "-d project_name='$project' ". | ||
| 82 | "-d task_name='$task' ". | ||
| 83 | "-d plugin_name='$plugin_name' ". | ||
| 84 | "-d comment='$comment' ", | ||
| 85 | silent => 1 , | ||
| 86 | ); | ||
| 87 | print "\n"; | ||
| 88 | |||
| 89 | execute_shell_command( | ||
| 90 | "curl -f -H 'sparrow-user: $cred->{user}' " . | ||
| 91 | "-H 'sparrow-token: $cred->{token}' " .sparrow_hub_api_url().'/api/v1/remote-task/load-ini'. | ||
| 92 | "/$project/$task/ ". | ||
| 93 | "-F ini=\@$suite_cfg_path ", | ||
| 94 | silent => 1 , | ||
| 95 | ); | ||
| 96 | print "\n"; | ||
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | sub remote_task_list { | ||
| 101 | |||
| 102 | my $cred; | ||
| 103 | |||
| 104 | if ($ENV{sph_user} and $ENV{sph_token}){ | ||
| 105 | $cred->{user} = $ENV{sph_user}; | ||
| 106 | $cred->{token} = $ENV{sph_token}; | ||
| 107 | } else { | ||
| 108 | # or read from $ENV{HOME}/sparrowhub.json | ||
| 109 | open F, "$ENV{HOME}/sparrowhub.json" or confess "can't open $ENV{HOME}/sparrowhub.json to read: $!"; | ||
| 110 | my $s = join "", <F>; | ||
| 111 | close F; | ||
| 112 | $cred = decode_json($s); | ||
| 113 | } | ||
| 114 | |||
| 115 | my $out_path = sparrow_root()."/cache/".($cred->{user}).".remote_tasks.json"; | ||
| 116 | |||
| 117 | execute_shell_command( | ||
| 118 | "curl -f -s -H 'sparrow-user: $cred->{user}' " . | ||
| 119 | "-H 'sparrow-token: $cred->{token}' -o $out_path " . | ||
| 120 | sparrow_hub_api_url().'/api/v1/remote-task/list', | ||
| 121 | silent => 1 , | ||
| 122 | ); | ||
| 123 | |||
| 124 | open my $fh, $out_path or die "can't $out_path to read: $!"; | ||
| 125 | my $json_str = join "", <$fh>; | ||
| 126 | close $fh; | ||
| 127 | |||
| 128 | for my $t (@{decode_json($json_str)}){ | ||
| 129 | my $access = $t->{public_access} ? 'public' : 'private'; | ||
| 130 | print "$t->{t} $access\t$t->{project_name}/$t->{task_name} | $t->{comment}\n" | ||
| 131 | } | ||
| 132 | print "\n"; | ||
| 133 | } | ||
| 134 | |||
| 135 | sub remote_task_public_list { | ||
| 136 | |||
| 137 | my $out_path = sparrow_root()."/cache/remote_tasks_public.json"; | ||
| 138 | |||
| 139 | execute_shell_command( | ||
| 140 | "curl -f -s -o $out_path " . | ||
| 141 | sparrow_hub_api_url().'/api/v1/remote-task/public-list', | ||
| 142 | silent => 1 , | ||
| 143 | ); | ||
| 144 | |||
| 145 | open my $fh, $out_path or die "can't $out_path to read: $!"; | ||
| 146 | my $json_str = join "", <$fh>; | ||
| 147 | close $fh; | ||
| 148 | |||
| 149 | for my $t (@{decode_json($json_str)}){ | ||
| 150 | my $access = $t->{public_access} ? 'public' : 'private'; | ||
| 151 | print "$t->{t} $t->{owner}\@$t->{project_name}/$t->{task_name} | $t->{comment}\n" | ||
| 152 | } | ||
| 153 | print "\n"; | ||
| 154 | } | ||
| 155 | |||
| 156 | sub remote_task_share { | ||
| 157 | remote_task_change_access(@_,'share'); | ||
| 158 | } | ||
| 159 | |||
| 160 | sub remote_task_hide { | ||
| 161 | remote_task_change_access(@_,'hide'); | ||
| 162 | } | ||
| 163 | |||
| 164 | sub remote_task_change_access { | ||
| 165 | |||
| 166 | my $path = shift or confess "usage: remote_task_change_access(path*,action)"; | ||
| 167 | my $action = shift or confess "usage: remote_task_change_access(path,*action)"; | ||
| 168 | |||
| 169 | my ($project, $task) = split '/', $path; | ||
| 170 | |||
| 171 | my $cred; | ||
| 172 | |||
| 173 | if ($ENV{sph_user} and $ENV{sph_token}){ | ||
| 174 | $cred->{user} = $ENV{sph_user}; | ||
| 175 | $cred->{token} = $ENV{sph_token}; | ||
| 176 | } else { | ||
| 177 | # or read from $ENV{HOME}/sparrowhub.json | ||
| 178 | open F, "$ENV{HOME}/sparrowhub.json" or confess "can't open $ENV{HOME}/sparrowhub.json to read: $!"; | ||
| 179 | my $s = join "", <F>; | ||
| 180 | close F; | ||
| 181 | $cred = decode_json($s); | ||
| 182 | } | ||
| 183 | |||
| 184 | |||
| 185 | execute_shell_command( | ||
| 186 | "curl -f -X POST -H 'sparrow-user: $cred->{user}' " . | ||
| 187 | "-H 'sparrow-token: $cred->{token}' " .sparrow_hub_api_url().'/api/v1/remote-task/'. | ||
| 188 | "$action/$project/$task", | ||
| 189 | silent => 1 , | ||
| 190 | ); | ||
| 191 | |||
| 192 | print "\n"; | ||
| 193 | } | ||
| 194 | |||
| 195 | sub remote_task_remove { | ||
| 196 | |||
| 197 | my $path = shift or confess "usage: remote_task_remove(path*)"; | ||
| 198 | |||
| 199 | my ($project, $task) = split '/', $path; | ||
| 200 | |||
| 201 | my $cred; | ||
| 202 | |||
| 203 | if ($ENV{sph_user} and $ENV{sph_token}){ | ||
| 204 | $cred->{user} = $ENV{sph_user}; | ||
| 205 | $cred->{token} = $ENV{sph_token}; | ||
| 206 | } else { | ||
| 207 | # or read from $ENV{HOME}/sparrowhub.json | ||
| 208 | open F, "$ENV{HOME}/sparrowhub.json" or confess "can't open $ENV{HOME}/sparrowhub.json to read: $!"; | ||
| 209 | my $s = join "", <F>; | ||
| 210 | close F; | ||
| 211 | $cred = decode_json($s); | ||
| 212 | } | ||
| 213 | |||
| 214 | |||
| 215 | execute_shell_command( | ||
| 216 | "curl -f -X POST -H 'sparrow-user: $cred->{user}' " . | ||
| 217 | "-H 'sparrow-token: $cred->{token}' " .sparrow_hub_api_url().'/api/v1/remote-task/remove/'. | ||
| 218 | "$project/$task", | ||
| 219 | silent => 1 , | ||
| 220 | ); | ||
| 221 | |||
| 222 | print "\n"; | ||
| 223 | } | ||
| 224 | |||
| 225 | sub remote_task_install { | ||
| 226 | |||
| 227 | my $path = shift or confess "usage: remote_task_install(path*,opts)"; | ||
| 228 | my %opts = @_; | ||
| 229 | |||
| 230 | my ($project, $task) = split '/', $path; | ||
| 231 | |||
| 232 | my $cred; | ||
| 233 | |||
| 234 | my $out_path = sparrow_root()."/cache/meta/$path.json"; | ||
| 235 | my $cfg_path = sparrow_root()."/cache/meta/$path.cfg"; | ||
| 236 | |||
| 237 | if ($project =~ s/^(\S+)@//){ | ||
| 238 | |||
| 239 | my $owner = $1; | ||
| 240 | |||
| 241 | my $unsecure_flag = $ENV{SPARROW_UNSECURE} ? "-k" : ""; | ||
| 242 | |||
| 243 | execute_shell_command( | ||
| 244 | "mkdir -p ".(sparrow_root())."/cache/meta/$owner\@$project && ". | ||
| 245 | "curl $unsecure_flag -f -s ". sparrow_hub_api_url().'/api/v1/remote-task/meta/'. | ||
| 246 | "$owner/$project/$task -o $out_path", | ||
| 247 | silent => 1 , | ||
| 248 | ); | ||
| 249 | |||
| 250 | } else { | ||
| 251 | if ($ENV{sph_user} and $ENV{sph_token}){ | ||
| 252 | $cred->{user} = $ENV{sph_user}; | ||
| 253 | $cred->{token} = $ENV{sph_token}; | ||
| 254 | } else { | ||
| 255 | # or read from $ENV{HOME}/sparrowhub.json | ||
| 256 | open F, "$ENV{HOME}/sparrowhub.json" or confess "can't read $ENV{HOME}/sparrowhub.json : $!"; | ||
| 257 | my $s = join "", <F>; | ||
| 258 | close F; | ||
| 259 | $cred = decode_json($s); | ||
| 260 | } | ||
| 261 | |||
| 262 | my $owner = $cred->{user}; | ||
| 263 | |||
| 264 | execute_shell_command( | ||
| 265 | "mkdir -p ".(sparrow_root())."/cache/meta/$project && ". | ||
| 266 | "curl -f -s -H 'sparrow-user: $cred->{user}' " . | ||
| 267 | "-H 'sparrow-token: $cred->{token}' " .sparrow_hub_api_url().'/api/v1/remote-task/meta/'. | ||
| 268 | "$owner/$project/$task -o $out_path", | ||
| 269 | silent => 1, | ||
| 270 | ); | ||
| 271 | |||
| 272 | } | ||
| 273 | |||
| 274 | print "task meta data saved to $out_path ...\n"; | ||
| 275 | |||
| 276 | open META, $out_path or confess "can't read $out_path : $!"; | ||
| 277 | my $str = join "", <META>; | ||
| 278 | close META; | ||
| 279 | my $meta = decode_json($str); | ||
| 280 | |||
| 281 | open INI, "> $cfg_path" or confess "can't write to $cfg_path : $!"; | ||
| 282 | print INI $meta->{suite_ini}; | ||
| 283 | close INI; | ||
| 284 | |||
| 285 | install_plugin($meta->{plugin_name}); | ||
| 286 | project_create($meta->{project_name}); | ||
| 287 | task_add($meta->{project_name},$meta->{task_name},$meta->{plugin_name}); | ||
| 288 | task_load_ini($meta->{project_name},$meta->{task_name},$cfg_path); | ||
| 289 | if ($opts{run}){ | ||
| 290 | task_run($meta->{project_name},$meta->{task_name}); | ||
| 291 | } else{ | ||
| 292 | print "\n\nnow you can ran task by: \$ sparrow task run $meta->{project_name} $meta->{task_name}\n\n"; | ||
| 293 | } | ||
| 294 | } | ||
| 295 | |||
| 296 | sub remote_task_run { | ||
| 297 | remote_task_install(@_, 'run' => 1); | ||
| 298 | } | ||
| 299 | |||
| 300 | 1; | ||
| 301 |