Repository: StackStorm/st2

Issue:
Pack reinstall fails if config.yaml is missing
Related to: https://github.com/StackStorm/st2contrib/pull/229

Reproduce:
Here is an empty pack which has no `config.yaml` file in it: https://github.com/armab/st2-pack-bug
#### 1. Pack install (with no `config.yaml`)

``` sh
# st2 run packs.install packs=st2-pack-bug repo_url=armab/st2-pack-bug
......................
id: 5586c6190a84b40a4a08a164
action.ref: packs.install
status: succeeded
start_timestamp: 2015-06-21T14:11:37.259169Z
end_timestamp: 2015-06-21T14:12:22.686647Z
+--------------------------+-----------+--------------------------+-------------------------+----------------------------+
| id                       | status    | task                     | action                  | start_timestamp            |
+--------------------------+-----------+--------------------------+-------------------------+----------------------------+
| 5586c61a0a84b40a402bdb88 | succeeded | download                 | packs.download          | Sun, 21 Jun 2015 14:11:38  |
|                          |           |                          |                         | UTC                        |
| 5586c62a0a84b40a402bdb8a | succeeded | virtualenv_prerun        | packs.virtualenv_prerun | Sun, 21 Jun 2015 14:11:54  |
|                          |           |                          |                         | UTC                        |
| 5586c62c0a84b40a402bdb8c | succeeded | setup_virtualenv         | packs.setup_virtualenv  | Sun, 21 Jun 2015 14:11:56  |
|                          |           |                          |                         | UTC                        |
| 5586c6370a84b40a402bdb8e | succeeded | reload                   | packs.load              | Sun, 21 Jun 2015 14:12:07  |
|                          |           |                          |                         | UTC                        |
| 5586c6410a84b40a402bdb90 | succeeded | restart-sensor-container | packs.restart_component | Sun, 21 Jun 2015 14:12:17  |
|                          |           |                          |                         | UTC                        |
+--------------------------+-----------+--------------------------+-------------------------+----------------------------+

```
#### 2. Pack reinstall (if pack already exists)

``` sh
# st2 run packs.install packs=st2-pack-bug repo_url=armab/st2-pack-bug
........
id: 5586c6700a84b40a4a08a166
action.ref: packs.install
status: failed
error: st2.actions.python.DownloadGitRepoAction: DEBUG    Removing existing pack st2-pack-bug in /opt/stackstorm/packs/st2-pack-bug to replace.
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/st2actions/runners/python_action_wrapper.py", line 116, in <module>
    obj.run()
  File "/usr/lib/python2.7/dist-packages/st2actions/runners/python_action_wrapper.py", line 61, in run
    output = action.run(**self._parameters)
  File "/opt/stackstorm/packs/packs/actions/pack_mgmt/download.py", line 65, in run
    result = self._move_packs(abs_repo_base, packs, pack_abs_local_path, self._subtree)
  File "/opt/stackstorm/packs/packs/actions/pack_mgmt/download.py", line 102, in _move_packs
    shutil.move(old_config_file, new_config_file)
  File "/usr/lib/python2.7/shutil.py", line 302, in move
    copy2(src, real_dst)
  File "/usr/lib/python2.7/shutil.py", line 130, in copy2
    copyfile(src, dst)
  File "/usr/lib/python2.7/shutil.py", line 82, in copyfile
    with open(src, 'rb') as fsrc:
IOError: [Errno 2] No such file or directory: u'/opt/stackstorm/packs/st2-pack-bug/config.yaml'

traceback: None
failed_on: download
start_timestamp: 2015-06-21T14:13:04.269175Z
end_timestamp: 2015-06-21T14:13:19.928860Z
+--------------------------+--------+----------+----------------+----------------------------+
| id                       | status | task     | action         | start_timestamp            |
+--------------------------+--------+----------+----------------+----------------------------+
| 5586c6700a84b40a3f312d06 | failed | download | packs.download | Sun, 21 Jun 2015 14:13:04  |
|                          |        |          |                | UTC                        |
+--------------------------+--------+----------+----------------+----------------------------+
```

---

As I understand the solution is or to make `config.yaml` as a requirement or to fix that reinstall problem.
№2 seems more logical, since config is not needed for all packs and it's used only by python action runners (what could be improved too btw).



Discussion:
@armab I agree with you that the second approach is better. I will look into it and thanks for the report.


Resolving patch:
diff --git a/contrib/packs/actions/pack_mgmt/download.py b/contrib/packs/actions/pack_mgmt/download.py
--- a/contrib/packs/actions/pack_mgmt/download.py
+++ b/contrib/packs/actions/pack_mgmt/download.py
@@ -96,11 +96,16 @@ def _move_packs(self, abs_repo_base, packs, abs_local_path, subtree):
                 if os.path.exists(dest_pack_path):
                     self.logger.debug('Removing existing pack %s in %s to replace.', pack,
                                       dest_pack_path)
+
                     # Ensure to preserve any existing configuration
                     old_config_file = os.path.join(dest_pack_path, CONFIG_FILE)
                     new_config_file = os.path.join(abs_pack_temp_location, CONFIG_FILE)
-                    shutil.move(old_config_file, new_config_file)
+
+                    if os.path.isfile(old_config_file):
+                        shutil.move(old_config_file, new_config_file)
+
                     shutil.rmtree(dest_pack_path)
+
                 self.logger.debug('Moving pack from %s to %s.', abs_pack_temp_location, to)
                 shutil.move(abs_pack_temp_location, to)
                 message = 'Success.'


Test patch: