add separate maas region and cluster definition
diff --git a/README.rst b/README.rst
index 3ef8b52..0f2b794 100644
--- a/README.rst
+++ b/README.rst
@@ -16,6 +16,38 @@
       server:
         enabled: true
 
+Single MAAS region service [single UI/API]
+
+.. code-block:: yaml
+
+    maas:
+      region:
+        enabled: true
+        bind:
+          host: localhost
+          port: 80
+        database:
+          engine: postgresql
+          host: localhost
+          name: maasdb
+          password: password
+          username: maas
+
+
+Single MAAS cluster service [multiple racks]
+
+.. code-block:: yaml
+
+    maas:
+      cluster:
+        enabled: true
+        version: '2'
+        region: 
+          host: localhost
+          port: 5240
+
+
+
 Read more
 =========
 
diff --git a/maas/cluster.sls b/maas/cluster.sls
new file mode 100644
index 0000000..072322f
--- /dev/null
+++ b/maas/cluster.sls
@@ -0,0 +1,17 @@
+{%- from "maas/map.jinja" import cluster with context %}
+{%- if cluster.enabled %}
+
+mass_cluster_packages:
+  pkg.installed:
+    - names: {{ cluster.pkgs }}
+
+{{ cluster.config.cluster }}:
+  file.line:
+  - content: 'maas_url: http://10.200.50.13/'
+  - match: 'maas_url*'
+  - mode: replace
+  - location: end
+  - require:
+    - pkg: mass_cluster_packages
+
+{%- endif %}
diff --git a/maas/files/clusterd.conf b/maas/files/clusterd.conf
new file mode 100644
index 0000000..8439618
--- /dev/null
+++ b/maas/files/clusterd.conf
@@ -0,0 +1,4 @@
+{%- from "maas/map.jinja" import server with context %}
+
+cluster_uuid: 9e11adab-1e52-499f-a5b7-7dd7e5ed3c95
+maas_url: http://10.200.50.13/MAAS
\ No newline at end of file
diff --git a/maas/files/curtin_userdata_amd64_generic_xenial b/maas/files/curtin_userdata_amd64_generic_xenial
new file mode 100644
index 0000000..94c3342
--- /dev/null
+++ b/maas/files/curtin_userdata_amd64_generic_xenial
@@ -0,0 +1,70 @@
+{%- from "maas/map.jinja" import server with context %}
+{% raw %}
+#cloud-config
+debconf_selections:
+ maas: |
+  {{for line in str(curtin_preseed).splitlines()}}
+  {{line}}
+  {{endfor}}
+{{if third_party_drivers and driver}}
+early_commands:
+  {{py: key_string = ''.join(['\\x%x' % x for x in map(ord, driver['key_binary'])])}}
+  driver_00_get_key: /bin/echo -en '{{key_string}}' > /tmp/maas-{{driver['package']}}.gpg
+  driver_01_add_key: ["apt-key", "add", "/tmp/maas-{{driver['package']}}.gpg"]
+  driver_02_add: ["add-apt-repository", "-y", "deb {{driver['repository']}} {{node.get_distro_series()}} main"]
+  driver_03_update_install: ["sh", "-c", "apt-get update --quiet && apt-get --assume-yes install {{driver['package']}}"]
+  driver_04_load: ["sh", "-c", "depmod && modprobe {{driver['module']}}"]
+{{endif}}
+late_commands:
+  maas: [wget, '--no-proxy', '{{node_disable_pxe_url|escape.shell}}', '--post-data', '{{node_disable_pxe_data|escape.shell}}', '-O', '/dev/null']
+{{if third_party_drivers and driver}}
+  driver_00_key_get: curtin in-target -- sh -c "/bin/echo -en '{{key_string}}' > /tmp/maas-{{driver['package']}}.gpg"
+  driver_02_key_add: ["curtin", "in-target", "--", "apt-key", "add", "/tmp/maas-{{driver['package']}}.gpg"]
+  driver_03_add: ["curtin", "in-target", "--", "add-apt-repository", "-y", "deb {{driver['repository']}} {{node.get_distro_series()}} main"]
+  driver_04_update_install: ["curtin", "in-target", "--", "apt-get", "update", "--quiet"]
+  driver_05_install: ["curtin", "in-target", "--", "apt-get", "-y", "install", "{{driver['package']}}"]
+  driver_06_depmod: ["curtin", "in-target", "--", "depmod"]
+  driver_07_update_initramfs: ["curtin", "in-target", "--", "update-initramfs", "-u"]
+{{endif}}
+  apt_00_tcpcloud_set: ["curtin", "in-target", "--", "sh", "-c", "echo 'deb [arch=amd64] http://apt.tcpcloud.eu/nightly/ trusty main security extra tcp tcp-salt' > /etc/apt/sources.list"]
+  apt_01_tcpcloud_gpg: ["curtin", "in-target", "--", "sh", "-c", "echo 'LS0tLS1CRUdJTiBQR1AgUFVCTElDIEtFWSBCTE9DSy0tLS0tDQpWZXJzaW9uOiBHbnVQRyB2MQ0KDQptUUlOQkZXQmZDSUJFQURmN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"update"]
+  apt_04_kernel: ["curtin", "in-target", "--", "apt-get", "install", "linux-headers-3.19.0-26", "linux-headers-3.19.0-26-generic", "linux-image-3.19.0-26-generic","linux-image-extra-3.19.0-26-generic"]
+  salt_00_announce: ["curtin", "in-target", "--", "echo", "'### Installing and Configuring Salt Minion ###'"]
+
+{% endraw %}
+
+  salt_01_install: ["curtin", "in-target", "--", "apt-get", "-y", "install", "salt-minion"]
+
+  salt_02_hostname_set: ["curtin", "in-target", "--", "echo", "{% raw %}{{node.hostname}}{% endraw %}.{{pillar.linux.system.domain}}"]
+  salt_03_hostname_get: ["curtin", "in-target", "--", "sh", "-c", "echo 'id: {% raw %}{{node.hostname}}{% endraw %}.{{pillar.linux.system.domain}}' >> /etc/salt/minion"]
+  salt_04_master: ["curtin", "in-target", "--", "sh", "-c", "echo 'master: 10.0.170.7' >> /etc/salt/minion"]
+  salt_05_announce: ["curtin", "in-target", "--", "echo", "'### Salt Minion is configured successfully ###'"]
+  salt_06_show_conf_hostname: ["curtin", "in-target", "--", "cat", "/etc/salt/minion"]
+  salt_07_show_hosts: ["curtin", "in-target", "--", "cat", "/etc/hosts"]
+
+{% raw %}
+power_state:
+  mode: reboot
+
+{{if node.split_arch()[0] in {'i386', 'amd64'} }}
+apt_mirrors:
+  ubuntu_archive: http://{{main_archive_hostname}}/{{main_archive_directory}}
+  ubuntu_security: http://{{main_archive_hostname}}/{{main_archive_directory}}
+{{else}}
+apt_mirrors:
+  ubuntu_archive: http://{{ports_archive_hostname}}/{{ports_archive_directory}}
+  ubuntu_security: http://{{ports_archive_hostname}}/{{ports_archive_directory}}
+{{endif}}
+
+{{if enable_http_proxy}}
+{{if http_proxy }}
+apt_proxy: {{http_proxy}}
+{{else}}
+apt_proxy: http://{{server_host}}:8000/
+{{endif}}
+{{endif}}
+
+{% endraw %}
+
diff --git a/maas/files/maas.conf b/maas/files/maas.conf
index ab6e25d..ae1a874 100644
--- a/maas/files/maas.conf
+++ b/maas/files/maas.conf
@@ -1,6 +1,12 @@
 {%- from "maas/map.jinja" import server with context %}
-database_host: localhost
-database_name: maasdb
-database_pass: a7Zpk1JVMn0Z
-database_user: maas
-maas_url: http://10.0.174.10/MAAS
+
+{% if server.database.engine == "postgresql" %}
+database_engine: 
+{% else %} 
+database_engine:
+{% endif %} 
+database_host: {{ server.database.host }}
+database_name: {{ server.database.name }}
+database_pass: {{ server.database.password }}
+database_user: {{ server.database.username }}
+maas_url: http://{{ server.region.host }}/MAAS
diff --git a/maas/files/regiond.conf b/maas/files/regiond.conf
new file mode 100644
index 0000000..15728f8
--- /dev/null
+++ b/maas/files/regiond.conf
@@ -0,0 +1,12 @@
+{%- from "maas/map.jinja" import region with context %}
+
+{% if region.database.engine == "postgresql" %}
+database_engine: 
+{% else %} 
+database_engine:
+{% endif %} 
+database_host: {{ region.database.host }}
+database_name: {{ region.database.name }}
+database_pass: {{ region.database.password }}
+database_user: {{ region.database.username }}
+maas_url: http://{{ region.bind.host }}:{{ region.bind.port }}/MAAS
diff --git a/maas/init.sls b/maas/init.sls
index 27b631f..30e3347 100644
--- a/maas/init.sls
+++ b/maas/init.sls
@@ -3,4 +3,10 @@
 {%- if pillar.maas.server is defined %}
 - maas.server
 {%- endif %}
+{%- if pillar.maas.cluster is defined %}
+- maas.cluster
+{%- endif %}
+{%- if pillar.maas.region is defined %}
+- maas.region
+{%- endif %}
 {%- endif %}
diff --git a/maas/map.jinja b/maas/map.jinja
index e293a9b..dfeb47c 100644
--- a/maas/map.jinja
+++ b/maas/map.jinja
@@ -2,11 +2,41 @@
 Debian:
   pkgs:
   - maas
-  - maas-region-controller
-  - maas-cluster-controller
   bind:
     address: 0.0.0.0
-    port: 8080
+    port: 80
 {%- endload %}
 
 {%- set server = salt['grains.filter_by'](base_defaults, merge=salt['pillar.get']('maas:server', {})) %}
+
+{%- set version = salt['pillar.get']('maas:cluster:version') %}
+
+{%- load_yaml as cluster_defaults %}
+Debian:
+  pkgs:
+  {% if version == '1' %}
+  - maas-cluster-controller
+  {% elif version == '2' %}
+  - maas-rack-controller
+  {% endif %}
+  config:
+  {% if version == '1' %}
+  - cluster: /etc/maas/cluster.conf
+  {% elif version == '2' %}
+  - cluster: /etc/maas/rackd.conf
+  {% endif %}
+{%- endload %}
+
+{%- set cluster = salt['grains.filter_by'](cluster_defaults, merge=salt['pillar.get']('maas:cluster', {})) %}
+
+{%- load_yaml as region_defaults %}
+Debian:
+  pkgs:
+  - maas-region-controller
+  service: maas
+  bind:
+    host: {{ grains.item.ivp4.0 }}
+    port: 80
+{%- endload %}
+
+{%- set region = salt['grains.filter_by'](region_defaults, merge=salt['pillar.get']('maas:region', {})) %}
\ No newline at end of file
diff --git a/maas/region.sls b/maas/region.sls
new file mode 100644
index 0000000..06f8bb4
--- /dev/null
+++ b/maas/region.sls
@@ -0,0 +1,25 @@
+{%- from "maas/map.jinja" import region with context %}
+{%- if region.enabled %}
+
+mass_region_packages:
+  pkg.installed:
+    - names: {{ region.pkgs }}
+
+/etc/maas/region.conf:
+  file.managed:
+  - source: salt://maas/files/region.conf
+  - template: jinja
+  - require:
+    - pkg: mass_region_packages
+
+/etc/maas/preseeds/curtin_userdata_amd64_generic_trusty:
+  file.managed:
+  - source: salt://maas/files/curtin_userdata_amd64_generic_trusty
+  - template: jinja
+  - user: root
+  - group: root
+  - mode: 644
+  - require:
+    - pkg: mass_region_packages
+
+{%- endif %}
diff --git a/maas/server.sls b/maas/server.sls
index 0d1851d..670baa3 100644
--- a/maas/server.sls
+++ b/maas/server.sls
@@ -5,6 +5,13 @@
   pkg.installed:
     - names: {{ server.pkgs }}
 
+/etc/maas/region.conf:
+  file.managed:
+  - source: salt://maas/files/region.conf
+  - template: jinja
+  - require:
+    - pkg: mass_region_packages
+
 /etc/maas/preseeds/curtin_userdata_amd64_generic_trusty:
   file.managed:
   - source: salt://maas/files/curtin_userdata_amd64_generic_trusty
diff --git a/metadata/service/cluster/single.yml b/metadata/service/cluster/single.yml
new file mode 100644
index 0000000..947b5f2
--- /dev/null
+++ b/metadata/service/cluster/single.yml
@@ -0,0 +1,12 @@
+applications:
+- maas
+classes:
+- service.maas.support
+parameters:
+  maas:
+    cluster:
+      enabled: true
+      version: '2'
+      region: 
+        host: localhost
+        port: 5240
diff --git a/metadata/service/region/single.yml b/metadata/service/region/single.yml
new file mode 100644
index 0000000..128ce96
--- /dev/null
+++ b/metadata/service/region/single.yml
@@ -0,0 +1,17 @@
+applications:
+- maas
+classes:
+- service.maas.support
+parameters:
+  maas:
+    region:
+      enabled: true
+      bind:
+        host: localhost
+        port: 80
+      database:
+        engine: postgresql
+        host: localhost
+        name: maasdb
+        password: password
+        username: maas
diff --git a/metadata/service/server/cluster.yml b/metadata/service/server/cluster.yml
index e231e4d..8194035 100644
--- a/metadata/service/server/cluster.yml
+++ b/metadata/service/server/cluster.yml
@@ -1,4 +1,4 @@
-applications:
+\applications:
 - maas
 classes:
 - service.maas.support
@@ -8,3 +8,4 @@
       enabled: true
       bind:
         address: 0.0.0.0
+    
\ No newline at end of file