init.pp 5.69 KB
Newer Older
Matthieu FAURE's avatar
Fix #9  
Matthieu FAURE committed
1
# @summary Install and configure DirectMairie
Matthieu FAURE's avatar
Matthieu FAURE committed
2
#
Matthieu FAURE's avatar
Fix #9  
Matthieu FAURE committed
3
# Install and configure DirectMairie, the open source citizen participation tool for cities streets
Matthieu FAURE's avatar
Matthieu FAURE committed
4 5
#
# @example
Matthieu FAURE's avatar
Fix #9  
Matthieu FAURE committed
6
#     class { 'directmairie':
7 8 9 10
#       smtp_host        => 'smtp.example.org',
#       smtp_port        => 587,
#       smtp_user_name   => 'KateBush@example.org',
#       smtp_user_passwd => 'RunningUpThatHill!',
Matthieu FAURE's avatar
Fix #9  
Matthieu FAURE committed
11 12
#       app_dir          => '/home/directmairie/DirectMairie',
#       picture_dir      => '/home/directmairie/DirectMairie-pictures',
13 14 15 16 17 18 19 20 21 22 23 24
#       security_key     => '0123654789abcdefghijklm',
#       ci_job_name      => 'myJob',
#       db_user_password => 'S3cr#TPassw0rdz',
#     }
#
# @param smtp_host Hostname of SMTP relay host
# @param smtp_port Port of SMTP relay host
# @param smtp_user_name Username for SMTP credentials
# @param smtp_user_passwd Password for SMTP credentials
# @param app_dir Directory in which application will be copied
# @param picture_dir Directory holding user-uploaded pictures (may be fat)
# @param security_key Secret key for the application (used as salt)
Matthieu FAURE's avatar
Fix #9  
Matthieu FAURE committed
25 26
# @param ci_job_name Gitlab CI job name from which to download DirectMairie jar file
# @param db_user_password Password for DirectMairie database
27 28
# @param package_list List of packages to be installed beforehand (stored in Hiera)

Matthieu FAURE's avatar
Fix #9  
Matthieu FAURE committed
29
class directmairie (
Matthieu FAURE's avatar
Matthieu FAURE committed
30 31 32 33
  String $smtp_host,
  Integer $smtp_port,
  String $smtp_user_name,
  String $smtp_user_passwd,
Matthieu FAURE's avatar
Fix #9  
Matthieu FAURE committed
34
  # Directory for DirectMairie application, eg /home/directmairie/DirectMairie
35
  String $app_dir,
Matthieu FAURE's avatar
Matthieu FAURE committed
36
  # Directory for users uploaded pictures
37
  String $picture_dir,
Matthieu FAURE's avatar
Matthieu FAURE committed
38
  # Secret key (used as salt)
39
  String $security_key,
Matthieu FAURE's avatar
Fix #9  
Matthieu FAURE committed
40
  # CI Job name from which downloading directmairie jar file (build artefact)
41
  String $ci_job_name,
Matthieu FAURE's avatar
Fix #9  
Matthieu FAURE committed
42
  # Password for DirectMairie database
43 44 45
  String $db_user_password,
  Array $package_list, # Value of $package_list are specified in Hiera

Matthieu FAURE's avatar
Fix #9  
Matthieu FAURE committed
46
  # Git refspec to download DirectMairie (can be branch, a tag, or a commit)
Matthieu FAURE's avatar
Matthieu FAURE committed
47
  String $refspec           = 'master',
48 49
  String $db_host           = 'localhost',
  Integer $db_port          = 5432,
Matthieu FAURE's avatar
Fix #9  
Matthieu FAURE committed
50 51 52 53
  String $db_name           = 'directmairie',
  String $db_for_tests_name = 'directmairie_test',
  String $db_user_name      = 'directmairie',
  String $system_username   = 'directmairie',
Matthieu FAURE's avatar
Matthieu FAURE committed
54
  # Activate debug / dev mode
55
  Boolean $debug            = false,
Matthieu FAURE's avatar
Matthieu FAURE committed
56 57 58 59 60
) {

  # #############################################################################
  # Variables

61
  $_gitlab_srv = 'gitlab.adullact.net'
Matthieu FAURE's avatar
Fix #9  
Matthieu FAURE committed
62 63
  $_gitlab_group = 'directmairie'
  $_gitlab_project_name = 'directmairie'
64

Matthieu FAURE's avatar
Fix #9  
Matthieu FAURE committed
65 66 67
  # URL of DirectMairie source code repository
  $_directmairie_gitlab_full_url = "${_gitlab_srv}/${_gitlab_group}/${_gitlab_project_name}"
  # URL of DirectMairie jar file (grabbed from a CI job)
Matthieu FAURE's avatar
Matthieu FAURE committed
68
  # see https://docs.gitlab.com/ee/user/project/pipelines/job_artifacts.html#downloading-the-latest-artifacts
Matthieu FAURE's avatar
Fix #9  
Matthieu FAURE committed
69
  $_directmairie_jar_url = join(
Matthieu FAURE's avatar
Matthieu FAURE committed
70
    [
Matthieu FAURE's avatar
Fix #9  
Matthieu FAURE committed
71
      "https://${_directmairie_gitlab_full_url}",
72
      "/-/jobs/artifacts/${refspec}/raw/backend/build/libs/amies.jar", # TODO change name once renamed in upstream
73
      "?job=${ci_job_name}",
Matthieu FAURE's avatar
Matthieu FAURE committed
74 75
    ]
  )
Matthieu FAURE's avatar
Fix #9  
Matthieu FAURE committed
76
  # Filename of DirectMairie jar file (build artefact)
77
  $jar_name = "directmairie-${refspec}.jar"
Matthieu FAURE's avatar
Matthieu FAURE committed
78

Matthieu FAURE's avatar
Fix #9  
Matthieu FAURE committed
79 80
  # Postgres role for DirectMairie
  $_directmairie_db_role = 'directmairie'
Matthieu FAURE's avatar
Matthieu FAURE committed
81
  # JDBC URL string
82
  $directmairie_jdbc_url = "jdbc:postgresql://${db_host}:${db_port}/${db_name}"
Matthieu FAURE's avatar
Matthieu FAURE committed
83 84 85 86

  # #############################################################################
  # PREREQUISITES packages

Matthieu FAURE's avatar
Fix #9  
Matthieu FAURE committed
87
  package { $directmairie::package_list:
Matthieu FAURE's avatar
Matthieu FAURE committed
88 89 90 91 92 93
    ensure => present,
  }

  # #############################################################################
  # PREREQUISITES Postgres

94
  # Role creation
Matthieu FAURE's avatar
Fix #9  
Matthieu FAURE committed
95
  postgresql::server::role { $_directmairie_db_role:
96
    password_hash => postgresql_password($db_user_name, $db_user_password),
Matthieu FAURE's avatar
Matthieu FAURE committed
97 98
  }

99 100 101 102
  # DB + Postgres user creation
  -> postgresql::server::db { $db_name:
    user     => $db_user_name,
    password => postgresql_password($db_user_name, $db_user_password),
Matthieu FAURE's avatar
Matthieu FAURE committed
103 104
  }

105 106
  # Privileges granting
  -> postgresql::server::database_grant { $db_name:
Matthieu FAURE's avatar
Matthieu FAURE committed
107
    privilege => 'ALL',
108
    db        => $db_name,
Matthieu FAURE's avatar
Fix #9  
Matthieu FAURE committed
109
    role      => $_directmairie_db_role,
Matthieu FAURE's avatar
Matthieu FAURE committed
110 111 112
  }

  # Create DB needed for running tests
Matthieu FAURE's avatar
Fix #9  
Matthieu FAURE committed
113
  if $directmairie::debug {
114 115 116
    postgresql::server::db { $db_for_tests_name:
      user     => $db_user_name,
      password => postgresql_password($db_user_name, $db_user_password),
Matthieu FAURE's avatar
Matthieu FAURE committed
117
    }
118
    postgresql::server::database_grant { $db_for_tests_name:
Matthieu FAURE's avatar
Matthieu FAURE committed
119
      privilege => 'ALL',
120
      db        => $db_for_tests_name,
Matthieu FAURE's avatar
Fix #9  
Matthieu FAURE committed
121 122
      role      => $_directmairie_db_role,
      require   => Postgresql::Server::Role[$_directmairie_db_role],
Matthieu FAURE's avatar
Matthieu FAURE committed
123 124 125 126
    }
  }

  # #############################################################################
Matthieu FAURE's avatar
Fix #9  
Matthieu FAURE committed
127
  # Prepare DirectMairie
Matthieu FAURE's avatar
Matthieu FAURE committed
128 129

  # Create directory for users uploaded pictures
Matthieu FAURE's avatar
Fix #9  
Matthieu FAURE committed
130
  file { 'DirectMairie picture dir':
Matthieu FAURE's avatar
Matthieu FAURE committed
131
    ensure => directory,
132
    path   => $picture_dir,
Matthieu FAURE's avatar
Matthieu FAURE committed
133
    mode   => '0700',
134
    owner  => $system_username,
Matthieu FAURE's avatar
Matthieu FAURE committed
135 136 137
  }

  # Create systemd unit-file
Matthieu FAURE's avatar
Fix #9  
Matthieu FAURE committed
138
  file { 'DirectMairie systemd unit':
Matthieu FAURE's avatar
Matthieu FAURE committed
139
    ensure  => file,
Matthieu FAURE's avatar
Fix #9  
Matthieu FAURE committed
140
    path    => '/etc/systemd/system/directmairie.service',
Matthieu FAURE's avatar
Matthieu FAURE committed
141
    mode    => '0644',
Matthieu FAURE's avatar
Fix #9  
Matthieu FAURE committed
142 143
    content => epp('directmairie/directmairie.service.epp'),
    require => Archive['download-directmairie-jar'],
144

Matthieu FAURE's avatar
Matthieu FAURE committed
145 146
  }

Matthieu FAURE's avatar
Fix #9  
Matthieu FAURE committed
147 148
  # Get DirectMairie jar file
  archive { 'download-directmairie-jar':
149
    ensure => 'present',
150
    path   => "${directmairie::app_dir}/${directmairie::jar_name}",
Matthieu FAURE's avatar
Fix #9  
Matthieu FAURE committed
151
    source => $_directmairie_jar_url,
Matthieu FAURE's avatar
Matthieu FAURE committed
152 153 154
  }

  # #############################################################################
Matthieu FAURE's avatar
Fix #9  
Matthieu FAURE committed
155
  # Launch DirectMairie
Matthieu FAURE's avatar
Matthieu FAURE committed
156

Matthieu FAURE's avatar
Fix #9  
Matthieu FAURE committed
157
  service { 'launch DirectMairie':
158
    ensure  => 'running',
Matthieu FAURE's avatar
Fix #9  
Matthieu FAURE committed
159
    name    => 'directmairie',
160
    enable  => true,
161
    require => [
Matthieu FAURE's avatar
Fix #9  
Matthieu FAURE committed
162 163
      Package[$directmairie::package_list],
      File['DirectMairie systemd unit'],
164 165
      Postgresql::Server::Database_grant[$db_name],
    ]
166
  }
Matthieu FAURE's avatar
Matthieu FAURE committed
167
}