Merge "Extend AssignMachinesIP,fix Machine define"
diff --git a/README.rst b/README.rst
index e3dba4f..fdfbb38 100644
--- a/README.rst
+++ b/README.rst
@@ -203,6 +203,16 @@
         enabled: true
         role: master/slave
 
+MAAS region service with backup data
+
+.. code-block:: yaml
+    maas:
+      region:
+        database:
+          initial_data:
+            source: cfg01.local
+            host: 192.168.0.11
+
 Module function's example:
 ==========================
 
@@ -296,4 +306,4 @@
 Any questions or feedback is always welcome so feel free to join our IRC
 channel:
 
-    #salt-formulas @ irc.freenode.net
+    #salt-formulas @ irc.freenode.net
\ No newline at end of file
diff --git a/_modules/maas.py b/_modules/maas.py
index c15ca33..ee110c8 100644
--- a/_modules/maas.py
+++ b/_modules/maas.py
@@ -52,7 +52,10 @@
     (9, 'Deploying'), (6, 'Deployed'), (7, 'Retired'), (8, 'Broken'),
     (11, 'Failed deployment'), (12, 'Releasing'),
     (13, 'Releasing failed'), (14, 'Disk erasing'),
-    (15, 'Failed disk erasing')])
+    (15, 'Failed disk erasing'), (16, 'Rescue mode'),
+    (17, 'Entering rescue mode'), (18, 'Failed to enter rescue mode'),
+    (19, 'Exiting rescue mode'), (20, 'Failed to exit rescue mode'),
+    (21, 'Testing'), (22, 'Failed testing')])
 
 
 def _format_data(data):
diff --git a/debian/control b/debian/control
index 9516048..9790e4c 100644
--- a/debian/control
+++ b/debian/control
@@ -10,6 +10,6 @@
 
 Package: salt-formula-maas
 Architecture: all
-Depends: ${misc:Depends}, salt-master, reclass
+Depends: ${misc:Depends}
 Description: maas salt formula
  Install and configure maas system.
diff --git a/maas/files/restore.sh b/maas/files/restore.sh
new file mode 100644
index 0000000..3d8e786
--- /dev/null
+++ b/maas/files/restore.sh
@@ -0,0 +1,42 @@
+{%- from "maas/map.jinja" import region with context}
+
+{%- set database = region.get("database", {}) %}
+
+{%- if database.host is defined %}
+{%- set pghost = database.get("host", "") %}
+export PGHOST={{ pghost }}
+{%- endif %}
+{%- if database.user is defined %}
+{%- set pguser = database.get("user", "") %}
+export PGUSER={{ pguser }}
+{%- endif %}
+export PGPASSFILE=/root/.pgpass
+
+{%- set db_name = database.get("name", "maasdb") %}
+{%- set backupninja_host = database.initial_data.get("host", grains.id ) %}
+{%- if database.initial_data.age is defined %}
+{%- set age = database.initial_data.get("age", "0") %}
+{%- else %}{%- set age = "0" %}{%- endif %}
+{%- set backupninja_source = database.initial_data.get("source", "cfg01.local")%}
+{%- set source_name = db_name + ".pg_dump.gz" %}
+{%- set dest_name = db_name + ".pg_dump.gz" %}
+{%- set target = "/root/postgresql/data/" %}
+
+
+scp backupninja@{{ backupninja_host }}:/srv/backupninja/{{ backupninja_source }}/var/backups/postgresql/postgresql.{{ age }}/{{ source_name }} {{ target }}{{ dest_name }} 
+gunzip -d -1 -f {{ target }}{{ dest_name }}
+
+scp -r backupninja@{{ backupninja_host }}:/srv/backupninja/{{ backupninja_source }}/etc/maas/maas.{{ age }} /etc/maas
+scp -r backupninja@{{ backupninja_host }}:/srv/backupninja/{{ backupninja_source }}/var/lib/maas/maas.{{ age }} /var/lib/maas
+
+sudo systemctl stop maas-dhcpd.service
+sudo systemctl stop maas-rackd.service
+sudo systemctl stop maas-regiond.service
+
+pg_restore {{ target }}{{ db_name }}.pg_dump --dbname={{ db_name }} --no-password -c
+
+touch /root/maas/flags/{{ db_name }}-installed
+
+sudo systemctl start maas-dhcpd.service
+sudo systemctl start maas-rackd.service
+sudo systemctl start maas-regiond.service
\ No newline at end of file
diff --git a/maas/region.sls b/maas/region.sls
index 554f9ba..a27142d 100644
--- a/maas/region.sls
+++ b/maas/region.sls
@@ -20,6 +20,24 @@
   - require:
     - pkg: maas_region_packages
 
+{%- if region.database.initial_data is defined %}
+
+/root/maas/scripts/restore_{{ region.database.name }}.sh:
+  file.managed:
+    - source: salt://maas/files/restore.sh
+    - mode: 770
+    - template: jinja
+
+restore_maas_database_{{ region.database.name }}:
+  cmd.run:
+  - name: /root/maas/scripts/restore_{{ region.database.name }}.sh
+  - unless: "[ -f /root/maas/flags/{{ region.database.name }}-installed ]"
+  - cwd: /root
+  - require:
+    - file: /root/maas/scripts/restore_{{ region.database.name }}.sh
+
+{%- endif %}
+
 {%- if region.get('enable_iframe', False)  %}
 
 /etc/apache2/conf-enabled/maas-http.conf:
diff --git a/metadata/service/cluster/single.yml b/metadata/service/cluster/single.yml
index 5c91bd1..a307dcc 100644
--- a/metadata/service/cluster/single.yml
+++ b/metadata/service/cluster/single.yml
@@ -41,5 +41,5 @@
         MA==
         =dtMN
         -----END PGP PUBLIC KEY BLOCK-----
-      saltstack_repo_trusty: "http://repo.saltstack.com/apt/ubuntu/16.04/amd64/2016.3/ trusty main"
-      saltstack_repo_xenial: "http://repo.saltstack.com/apt/ubuntu/16.04/amd64/2016.3/ xenial main"
\ No newline at end of file
+      saltstack_repo_trusty: "http://repo.saltstack.com/apt/ubuntu/14.04/amd64/2016.3/ trusty main"
+      saltstack_repo_xenial: "http://repo.saltstack.com/apt/ubuntu/16.04/amd64/2016.3/ xenial main"
diff --git a/tests/run_tests.sh b/tests/run_tests.sh
index 29fb975..4ee4e0f 100755
--- a/tests/run_tests.sh
+++ b/tests/run_tests.sh
@@ -124,7 +124,7 @@
 }
 
 salt_run() {
-    [ -e ${VEN_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate
+    [ -e ${VENV_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate
     salt-call ${SALT_OPTS} $*
 }