Config switcher for proprietary Incognito BCC client JIMC.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

jimcswitcher.pl 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/perl
  2. use strict;
  3. use File::Basename;
  4. use Data::Dumper;
  5. use warnings;
  6. my $path = dirname($0);
  7. my %conf = (
  8. 'jimc' => "jimc.jnlp",
  9. 'regions' => "$path/regions",
  10. 'deployconf' => "$ENV{HOME}/.jimc",
  11. 'defaults' => "$path/defaults",
  12. );
  13. my $regions_list = list_regions();
  14. my $selected_region = select_region($regions_list);
  15. print "REGION: $selected_region\n";
  16. deploy_region($selected_region);
  17. start_jimc($selected_region);
  18. print "NOTE: Close JIMC before saving the region!\n";
  19. print "Do you want to save the region? [y/N] ";
  20. my $sel = <STDIN>;
  21. if ( defined $sel && $sel =~/[yY]/ ) {
  22. save_region($selected_region);
  23. print "Region $selected_region saved\n";
  24. }
  25. print "Done\n";
  26. sub list_regions {
  27. my %r;
  28. opendir(DIR,$conf{regions}) || die "directory $conf{regions} does not exist";
  29. while(readdir DIR) {
  30. next if /^\./;
  31. my $dir = $_;
  32. opendir(DIR2,$conf{regions}.'/'.$dir) || next;
  33. while(readdir DIR2) {
  34. next if /^\./;
  35. my $file = $_;
  36. push @{$r{$dir}}, $file;
  37. }
  38. closedir(DIR2);
  39. }
  40. closedir(DIR);
  41. return \%r;
  42. }
  43. sub select_region {
  44. my($regions) = @_;
  45. my $select;
  46. my $region;
  47. while(1) {
  48. print "=============================================================================\n";
  49. print "== SELECT REGION TO START ==\n";
  50. print "=============================================================================\n";
  51. my @idx;
  52. foreach my $r ( sort keys %{$regions} ) {
  53. push @idx, $r;
  54. printf("== %s) %s\n", scalar(@idx), $r);
  55. }
  56. print "=============================================================================\n";
  57. print "Which region do you want to start? : ";
  58. my $select = <STDIN>;
  59. chomp $select;
  60. if ( defined $select && $select =~ /^\d+$/ && defined $idx[$select-1] ) {
  61. $region = $idx[$select-1];
  62. last;
  63. }
  64. }
  65. return $region;
  66. }
  67. sub deploy_region {
  68. my($region) = @_;
  69. system("rm -f $conf{deployconf}/*");
  70. system("cp $conf{defaults}/* $conf{deployconf}/");
  71. system("cp $conf{regions}/$region/* $conf{deployconf}/");
  72. }
  73. sub start_jimc {
  74. my($region) = @_;
  75. my $jimc = "$conf{regions}/$region/$conf{jimc}" ?
  76. "$conf{regions}/$region/$conf{jimc}" : "$conf{defaults}/$conf{jimc}";
  77. system("open $jimc");
  78. }
  79. sub save_region {
  80. my($region) = @_;
  81. opendir(CONF,$conf{deployconf}) || die "failed to open jimc config dir $conf{deployconf}";
  82. while(readdir CONF) {
  83. next if /^\./;
  84. my $file = $_;
  85. if ( -r "$conf{regions}/$region/$file" ) {
  86. print "Copying $file to region $region\n";
  87. system("cp $conf{deployconf}/$file $conf{regions}/$region/$file");
  88. }
  89. }
  90. closedir(CONF);
  91. }
  92. sub file_ts {
  93. my($file) = @_;
  94. if ( -r $file ) {
  95. my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($file);
  96. return $mtime > $ctime ? $mtime : $ctime;
  97. }
  98. return 0;
  99. }