| 1 | package Sparrow::Misc; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | use base 'Exporter'; | ||
| 6 | use Carp; | ||
| 7 | use YAML qw{LoadFile}; | ||
| 8 | |||
| 9 | use File::Path qw(make_path remove_tree); | ||
| 10 | use File::Touch; | ||
| 11 | use HTTP::Tiny; | ||
| 12 | |||
| 13 | use Sparrow::Constants; | ||
| 14 | |||
| 15 | our @EXPORT = qw { | ||
| 16 | execute_shell_command | ||
| 17 | usage | ||
| 18 | init_sparrow_env | ||
| 19 | sparrow_config | ||
| 20 | get_http_resource | ||
| 21 | }; | ||
| 22 | |||
| 23 | my $sparrow_config; | ||
| 24 | |||
| 25 | sub execute_shell_command { | ||
| 26 | |||
| 27 | my $cmd = shift; | ||
| 28 | my %opts = @_; | ||
| 29 | |||
| 30 | my $st = ( system($cmd) == 0 ); | ||
| 31 | |||
| 32 | print "run cmd: $cmd ...\n" if $opts{debug}; | ||
| 33 | |||
| 34 | if ($opts{silent}){ | ||
| 35 | die "failed to execute shell command" unless $st; | ||
| 36 | } else { | ||
| 37 | confess "failed to execute shell command: $cmd" unless $st; | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | sub usage { | ||
| 42 | |||
| 43 | print "usage: sparrow index|plg|project|task action args\n\n"; | ||
| 44 | |||
| 45 | print "action examples:\n\n"; | ||
| 46 | |||
| 47 | print "\[index]:\n\n"; | ||
| 48 | print "\tsparrow index update # get freash index from SparrowHub\n"; | ||
| 49 | print "\tsparrow index summary # print cached index summary\n"; | ||
| 50 | |||
| 51 | print "\n[projects]:\n\n"; | ||
| 52 | print "\tsparrow project create foo # create a project\n"; | ||
| 53 | print "\tsparrow project remove foo # remove a project\n"; | ||
| 54 | print "\tsparrow project show foo # get project info\n"; | ||
| 55 | print "\tsparrow project list # list projects\n"; | ||
| 56 | |||
| 57 | print "\n[plugins]:\n\n"; | ||
| 58 | |||
| 59 | print "\tsparrow plg search foo # search plugins \n"; | ||
| 60 | print "\tsparrow plg list # show installed plugins \n"; | ||
| 61 | print "\tsparrow plg install df-check # install plugin \n"; | ||
| 62 | print "\tsparrow plg remove df-check # remove plugin\n"; | ||
| 63 | print "\tsparrow plg show df-check # get plugin info\n"; | ||
| 64 | print "\tsparrow plg man df-check # get plugin manual\n"; | ||
| 65 | print "\tsparrow plg run df-check # run plguin tests\n"; | ||
| 66 | |||
| 67 | |||
| 68 | print "\n[tasks]:\n\n"; | ||
| 69 | print "\tsparrow task add system disk df-check # create task named `disk' tied to plugin df-check in project system\n"; | ||
| 70 | print "\tsparrow task remove system disk # remove task named `disk' in project foo\n"; | ||
| 71 | print "\tsparrow task show system disk # get `disk' task info \n"; | ||
| 72 | print "\tsparrow task run system disk # run `disk' task \n"; | ||
| 73 | print "\tsparrow task ini system disk # populate task plugin configuration \n"; | ||
| 74 | print "\tsparrow task load_ini system disk /path/to/ini/file # load plugin configuration from file \n"; | ||
| 75 | print "\tsparrow task list # get tasks and projects list \n"; | ||
| 76 | print "\tsparrow task save /var/data/tasks # save current tasks to directory \n"; | ||
| 77 | print "\tsparrow task restore /var/data/tasks # restore tasks from directory \n"; | ||
| 78 | print "\tsparrow task copy p1/t1 p2/t2 # copy task between projects \n"; | ||
| 79 | |||
| 80 | print "\n[task boxes]:\n\n"; | ||
| 81 | print "\tsparrow box run /path/to/box.json # run task box \n"; | ||
| 82 | |||
| 83 | print "\n[remote tasks]:\n\n"; | ||
| 84 | print "\tsparrow remote task upload utils/git-setup # upload task named `git-setup' to your SparrowHub account\n"; | ||
| 85 | print "\tsparrow remote task install utils/git-setup # install your remote task utils/git-setup\n"; | ||
| 86 | print "\tsparrow remote task install john\@utils/git-setup # install John's remote task utils/git-setup\n"; | ||
| 87 | print "\tsparrow remote task share utils/git-setup # share task named `git-setup' \n"; | ||
| 88 | print "\tsparrow remote task hide utils/git-setup # hide task named `git-setup' \n"; | ||
| 89 | print "\tsparrow remote task list # list your remote tasks\n"; | ||
| 90 | print "\tsparrow remote task public list # list public remote tasks\n"; | ||
| 91 | print "\tsparrow remote task remove utils/git-setup # remove remote task named `git-setup' \n"; | ||
| 92 | |||
| 93 | print "\n\n"; | ||
| 94 | |||
| 95 | print "follow https://github.com/melezhik/sparrow to get full documentation\n"; | ||
| 96 | |||
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | |||
| 101 | |||
| 102 | sub init_sparrow_env { | ||
| 103 | |||
| 104 | make_path(sparrow_root()); | ||
| 105 | |||
| 106 | print "sparrow root: [", sparrow_root(),"]\n"; | ||
| 107 | |||
| 108 | make_path(sparrow_root().'/plugins', { verbose => 1 } ); # | ||
| 109 | make_path(sparrow_root().'/plugins/private'); | ||
| 110 | make_path(sparrow_root().'/plugins/public'); | ||
| 111 | make_path(sparrow_root().'/projects'); | ||
| 112 | #remove_tree(sparrow_root.'/cache'); | ||
| 113 | |||
| 114 | make_path(sparrow_root.'/cache'); | ||
| 115 | |||
| 116 | touch( spi_file(), spl_file(), spci_file() ); | ||
| 117 | |||
| 118 | if (-f sparrow_conf_file()){ | ||
| 119 | ($sparrow_config) = LoadFile(sparrow_conf_file()); | ||
| 120 | } | ||
| 121 | } | ||
| 122 | |||
| 123 | sub sparrow_config { | ||
| 124 | $sparrow_config||{}; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub get_http_resource { | ||
| 128 | |||
| 129 | my $url = shift; | ||
| 130 | my %opts = @_; | ||
| 131 | |||
| 132 | my $agent = $opts{agent} || 'http-tiny'; | ||
| 133 | my $res; | ||
| 134 | |||
| 135 | if ( -f $url ) { | ||
| 136 | open my $fh1, $url or die "can't open $url to read: $!"; | ||
| 137 | $res = join "", <$fh1>; | ||
| 138 | close $fh1; | ||
| 139 | } else { | ||
| 140 | my $a = 1; | ||
| 141 | my $s = 1; | ||
| 142 | while (1){ | ||
| 143 | my $response = HTTP::Tiny->new( agent => $agent )->get($url); | ||
| 144 | if ($response->{success}){ | ||
| 145 | $res = $response->{content}; | ||
| 146 | last; | ||
| 147 | } elsif( $response->{code} == 599 ) { # retry N=3 times in case of "Network Connect Timeout" Error | ||
| 148 | $a++; | ||
| 149 | next if $a > 3; # N=3 | ||
| 150 | $s = $s*2; | ||
| 151 | print "retrying to fetch the url ... attempt number $a, sleep $s seconds before\n"; | ||
| 152 | sleep($s); | ||
| 153 | } | ||
| 154 | die "Failed to fetch $url: $response->{status} $response->{reason}\n"; | ||
| 155 | } | ||
| 156 | } | ||
| 157 | return $res; | ||
| 158 | } | ||
| 159 | |||
| 160 | 1; | ||
| 161 |