snapshot 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #!/usr/bin/env bash
  2. # __rvm_detect_system() does not support AIX
  3. sys=$( command uname -s )
  4. if [[ "${sys}" == AIX ]] ; then
  5. name_opt=-name
  6. else
  7. name_opt=-iname
  8. fi
  9. unset GREP_COLOR
  10. unset GREP_OPTIONS
  11. source "$rvm_scripts_path/base"
  12. __error_on_result()
  13. {
  14. if [[ "$1" -gt 0 ]]; then
  15. rvm_error "$2 - Aborting now."
  16. return 0
  17. else
  18. return 1
  19. fi
  20. }
  21. snapshot_save()
  22. {
  23. \typeset snapshot_temp_path snapshot_ruby_name_file \
  24. snapshot_alias_name_file snapshot_installable_file \
  25. snapshot_primary_ruby snapshot_ruby_order destination_path
  26. if [[ -z "$1" ]]
  27. then
  28. printf "%b" "
  29. Usage:
  30. rvm snapshot save name
  31. Description:
  32. Saves a snapshot describing the rvm installation
  33. to <name>.tar.gz in the current working directory.\
  34. " >&2
  35. return 1
  36. fi
  37. # Create the temporary directory.
  38. snapshot_temp_path="${rvm_tmp_path}/$$-snapshot"
  39. __rvm_rm_rf "$snapshot_temp_path"
  40. mkdir -p "$snapshot_temp_path"
  41. rvm_log "Backing up a list of aliases"
  42. __rvm_cp "$rvm_path/config/alias" "$snapshot_temp_path/"
  43. rvm_log "Backing up your user preferences"
  44. __rvm_cp "$rvm_user_path/db" "$snapshot_temp_path/"
  45. rvm_log "Backing up your installed packages"
  46. __rvm_sed -e 's/-//' -e 's/^lib//' < "$rvm_path/config/pkg" | __rvm_awk -F= '{print $1}' | sort | uniq > "$snapshot_temp_path/pkg"
  47. rvm_log "Backing up all of your gemsets"
  48. mkdir -p "$snapshot_temp_path/gems"
  49. (
  50. __rvm_cd "$snapshot_temp_path/gems"
  51. for snapshot_gemset in $(__rvm_list_gemset_strings)
  52. do
  53. __rvm_become "$snapshot_gemset" ; result="$?"
  54. __error_on_result "$result" "Error becoming ruby $snapshot_gemset" && return "$result"
  55. "$rvm_scripts_path/gemsets" export "${snapshot_gemset}.gems" >/dev/null ; result="$?"
  56. __error_on_result "$result" "Error exporting gemset contents for $snapshot_gemset" && return "$result"
  57. mkdir -p "./$snapshot_gemset/"
  58. [[ -d "$GEM_HOME/cache/" ]] && __rvm_cp -R "$GEM_HOME/cache/" "./$snapshot_gemset/"
  59. done
  60. )
  61. rvm_log "Backing up all of your installed rubies"
  62. printf "%b" "#!/usr/bin/env bash\n\nset -e\n\n" > "$snapshot_temp_path/install-rubies.sh"
  63. echo "source \"\$rvm_scripts_path/rvm\" || true" >> "$snapshot_temp_path/install-rubies.sh"
  64. snapshot_ruby_name_file="${rvm_tmp_path}/$$-rubies"
  65. snapshot_alias_name_file="${rvm_tmp_path}/$$-aliases"
  66. snapshot_installable_file="${rvm_tmp_path}/$$-installable"
  67. "$rvm_scripts_path/alias" list | __rvm_awk -F ' => ' '{print $1}' | sort | uniq 2>/dev/null > "$snapshot_alias_name_file"
  68. __rvm_list_strings | \command \tr ' ' '\n' | sort | uniq > "$snapshot_ruby_name_file"
  69. comm -2 -3 "$snapshot_ruby_name_file" "$snapshot_alias_name_file" > "$snapshot_installable_file"
  70. __rvm_rm_rf "$snapshot_ruby_name_file"
  71. __rvm_rm_rf "$snapshot_alias_name_file"
  72. snapshot_primary_ruby="$(__rvm_grep '^\(ree\|ruby-1.8.7\)' < "$snapshot_installable_file" | __rvm_grep -v '-head$' | \command \sort -r | \command \head -n1)"
  73. snapshot_ruby_order="$snapshot_primary_ruby $(__rvm_grep -v "$snapshot_primary_ruby" < "$snapshot_installable_file")"
  74. for snapshot_ruby_name in $snapshot_ruby_order
  75. do
  76. snapshot_install_command="$(__rvm_recorded_install_command "$snapshot_ruby_name")"
  77. if [[ -n "$snapshot_install_command" ]]
  78. then
  79. echo "rvm install $snapshot_install_command" | __rvm_sed "s#$rvm_path#'\\\"\$rvm_path\\\"'#" >> "$snapshot_temp_path/install-rubies.sh"
  80. else
  81. __rvm_become "$snapshot_ruby_name"
  82. ruby "$rvm_path/lib/rvm/install_command_dumper.rb" >> "$snapshot_temp_path/install-rubies.sh"
  83. fi
  84. unset snapshot_install_command
  85. done
  86. unset snapshot_ruby_name snapshot_primary_ruby
  87. __rvm_rm_rf "$snapshot_installable_file"
  88. rvm_log "Compressing snapshotting"
  89. destination_path="$PWD"
  90. (
  91. __rvm_cd "$snapshot_temp_path"
  92. __rvm_rm_rf "$destination_path/$1.tar.gz"
  93. __rvm_tar czf "$destination_path/$1.tar.gz" .
  94. result="$?"
  95. __error_on_result "$result" "Error creating archive $destination_path/$1.tar.gz" && return "$result"
  96. )
  97. rvm_log "Cleaning up"
  98. __rvm_rm_rf "$snapshot_temp_path"
  99. rvm_log "Snapshot complete"
  100. }
  101. snapshot_load()
  102. {
  103. \typeset package_info snapshot_archive snapshot_temp_path \
  104. alias_name alias_ruby
  105. export rvm_create_flag
  106. if [[ -z "$1" ]]
  107. then
  108. echo "Usage: rvm snapshot load name" >&2
  109. echo "Loads a snapshot from <name>.tar.gz in the current directory." >&2
  110. return 1
  111. fi
  112. snapshot_archive="$PWD/$(echo "$1" | __rvm_sed 's/.tar.gz$//').tar.gz"
  113. if ! [[ -s "$snapshot_archive" ]]
  114. then
  115. echo "The provides snapshot '$(basename "$snapshot_archive")' doesn't exist." >&2
  116. return 1
  117. fi
  118. snapshot_temp_path="${rvm_tmp_path}/$$-snapshot"
  119. __rvm_rm_rf "$snapshot_temp_path"
  120. \command \mkdir -p "$snapshot_temp_path"
  121. rvm_log "Extracting snapshot"
  122. (
  123. __rvm_cd "$snapshot_temp_path"
  124. __rvm_tar xzf "$snapshot_archive"
  125. result="$?"
  126. __error_on_result "$result" "Error extracting the archive '$snapshot_archive'" && return "$result"
  127. )
  128. rvm_log "Restoring user settings"
  129. __rvm_cp -f "$snapshot_temp_path/db" "$rvm_user_path/db"
  130. rvm_log "Installing rvm-managed packages"
  131. for snapshot_package in $(\command \cat "$snapshot_temp_path/pkg")
  132. do
  133. "$rvm_scripts_path/package" install "$snapshot_package"
  134. result="$?"
  135. __error_on_result "$result" "Error installing package '$snapshot_package'" && return "$result"
  136. done
  137. unset snapshot_package
  138. rvm_log "Installing rubies"
  139. chmod +x "$snapshot_temp_path/install-rubies.sh"
  140. __rvm_sed_i "${snapshot_temp_path}/install-rubies.sh" -e '1 s/#!\/usr\/bin\/env bash -e/#!\/usr\/bin\/env bash\n\nset -e/'
  141. "$snapshot_temp_path/install-rubies.sh"
  142. result="$?"
  143. __error_on_result "$result" "Error importing rubies." && return "$result"
  144. rvm_create_flag=1
  145. rvm_log "Setting up gemsets"
  146. (
  147. __rvm_cd "$snapshot_temp_path/gems"
  148. gems=($(__rvm_find . -mindepth 0 -maxdepth 0 -type f "${name_opt}" '*.gems' | __rvm_sed 's/.gems$//'))
  149. for snapshot_gemset in "${gems[@]//.\/}"
  150. do
  151. __rvm_become "$snapshot_gemset"
  152. result="$?"
  153. __error_on_result "$result" \
  154. "Error becoming '$snapshot_gemset'" && return "$result"
  155. mkdir -p "$GEM_HOME/cache/"
  156. __rvm_cp -Rf "$snapshot_gemset/" "$GEM_HOME/cache/"
  157. result="$?"
  158. __error_on_result "$result" \
  159. "Error copying across cache for $snapshot_gemset" && return "$result"
  160. "$rvm_scripts_path/gemsets" import "$snapshot_gemset" >/dev/null 2>&1
  161. result="$?"
  162. __error_on_result "$result" \
  163. "Error importing gemset for $snapshot_gemset" && return "$result"
  164. done
  165. )
  166. rvm_log "Restoring aliases"
  167. while read -r package_info
  168. do
  169. # Note: this assumes an '=' int the input...
  170. alias_name="${package_info/=*}"
  171. alias_ruby="${package_info/*=}"
  172. "$rvm_scripts_path/alias" create "$alias_name" "$alias_ruby"
  173. done < "$snapshot_temp_path/alias"
  174. rvm_log "Cleaning up load process"
  175. __rvm_rm_rf "$snapshot_temp_path"
  176. rvm_log "Loaded snapshot from $(basename "$snapshot_archive")"
  177. }
  178. snapshot_usage()
  179. {
  180. echo "Usage: rvm snapshot {save,load} file" >&2
  181. return 1
  182. }
  183. args=($*)
  184. action="${args[0]}"
  185. args="$(echo ${args[@]:1})" # Strip trailing / leading / extra spacing.
  186. case "$action" in
  187. save) snapshot_save "$args" ;;
  188. load) snapshot_load "$args" ;;
  189. *) snapshot_usage ;;
  190. esac
  191. exit $?