summaryrefslogtreecommitdiffstats
path: root/manifests/init.pp
blob: e4386b00a58c045c7157023b06e3e0e193adfa58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# mysql.pp
# Copyright (C) 2007 David Schmitt <david@schmitt.edv-bus.at>
# See LICENSE for the full license granted to you.

import "variables.pp"
import "passwords"

class mysql::server {
        include passwords
	include variables

	package { "mysql-server":
            name   => "MySQL-server-community",
	    ensure => installed,
	}

	package { "mysql-client":
            name   => "MySQL-client-community",
	    ensure => installed,
	}

	service { "mysql":
		ensure => running,
		hasstatus => true,
		require => Package["mysql-server"],
	}

        # It is more convenient to set the root password with 
        # and exec than using the plugin

        $mysql_root_user       = $variables::mysql_root_user
        $mysql_root_database   = $variables::mysql_root_database
        $mysql_root_password   = $passwords::mysql_root_password
        $mysql_root_local_host = $variables::mysql_root_local_host
        $mysql_global_host     = $variables::mysql_global_host

        $mysql_replication_user = $variables::mysql_replication_user
        $mysql_replication_password = $passwords::mysql_replication_password


        $mysql_cmd_root_without_pwd = "/usr/bin/mysql --user=$mysql_root_user --database=$mysql_root_database --host=$mysql_root_local_host"
        $mysql_cmd_root_with_pwd    = "/usr/bin/mysql --user=$mysql_root_user --database=$mysql_root_database --host=$mysql_root_local_host --password=$mysql_root_password"
        $mysql_cmd_repl_with_pwd    = "/usr/bin/mysql --user=$mysql_replication_user --database=$mysql_root_database --host=$mysql_root_local_host --password=$mysql_replication_password"

        exec { "mysql root password init":
            command     => "$mysql_cmd_root_without_pwd --execute=\"Update user set password=password('$mysql_root_password') where user='$mysql_root_user';\"",
            unless      => "$mysql_cmd_root_with_pwd --execute '\s'",
            require     => Service["mysql"],
        }
        
        exec { "mysql root flush password":
            command     => "$mysql_cmd_root_without_pwd --execute=\"flush privileges;\"",
            unless      => "$mysql_cmd_root_with_pwd --execute '\s'",
            require     => Exec["mysql root password init"],
        }

        exec {"mysql create replication user":
            command     => "$mysql_cmd_root_with_pwd --execute=\"Create user '$mysql_replication_user'@'$mysql_global_host' identified by '$mysql_replication_password';\"",
            unless      => "$mysql_cmd_repl_with_pwd --execute '\s'",
            require     => Exec["mysql root flush password"],
        }

        exec { "grants all to replication user":
            command     => "$mysql_cmd_root_with_pwd --execute=\"GRANT All PRIVILEGES  ON *.* TO '$mysql_replication_user'@'$mysql_global_host' IDENTIFIED BY '$mysql_replication_password';\"",
            unless      => "$mysql_cmd_repl_with_pwd --execute '\s'",
            require     => Exec["mysql create replication user"],
        }

        exec { "mysql root flush grants":
            command     => "$mysql_cmd_root_without_pwd --execute=\"flush privileges;\"",
            unless      => "$mysql_cmd_root_with_pwd --execute '\s'",
            require     => Exec["grants all to replication user"],
        }
}

class mysql::standalone inherits mysql::server {
    mysql::mysql_config {"set binary logging":
         binary_logging => false,
    }
}

class mysql::m2s inherits mysql::server {
    mysql::mysql_config {"set binary logging":
         binary_logging => true,
    } 
    
    exec { "restart mysql server":
         command     => "service mysql restart",
         unless      => "$mysql_cmd_repl_with_pwd --execute=\"show master status;\" | grep mysqllog",
         require     => Service ["mysql"],
    }

}

class mysql::slave inherits mysql::server {
    mysql::mysql_config {"set binary logging":
         binary_logging => false,
    }

    mysql::mysql_replication {"Configure Slave Server for Replication":}
}

class mysql::m2m inherits mysql::server {
    mysql::mysql_config {"set binary logging":
         binary_logging => true,
    }

    mysql::mysql_replication {"Configure Master Server for Master to Master Replication": }
}

define mysql::mysql_config($binary_logging){

    file { "/etc/my.cnf":
         ensure    => present,
         owner     => "mysql", 
         group     => "mysql", 
         mode      => 0644, 
         content   => template("mysql/my.cnf.erb"),
         before    => Service["mysql"],
    }
}

define mysql::mysql_replication{

    file { "/var/lib/mysql/gather_master_data.bash":
         owner       => "mysql",
         group       => "mysql",
         ensure      => present,
         mode        => 0755,
         content     => template("mysql/gather_master_data.bash.erb"),
         require     => Exec["grants all to replication user"],
    }

    exec { "remove set master datafile":
         command     => "rm /var/lib/mysql/set_master_repl_data.sql",
         unless      => "$mysql_cmd_repl_with_pwd --execute=\"show slave status;\" | grep Wait",
         require     => File["/var/lib/mysql/gather_master_data.bash"],
    }

    exec { "set master data for slave":
         command     => "/var/lib/mysql/gather_master_data.bash",
         creates     => "/var/lib/mysql/set_master_repl_data.sql",
         unless      => "$mysql_cmd_repl_with_pwd --execute=\"show slave status;\" | grep Wait",
         require     => File["/var/lib/mysql/gather_master_data.bash"],
    }

    exec { "restart mysql server":
         command     => "service mysql restart",
         unless      => "$mysql_cmd_repl_with_pwd --execute=\"show slave status;\" | grep Wait",
         require     => Exec["set master data for slave"],
    }

    exec { "start slave server":
         command     => "$mysql_cmd_repl_with_pwd --execute=\"start slave;\"",
         unless      => "$mysql_cmd_repl_with_pwd --execute=\"show slave status;\" | grep Wait",
         require     => Exec["set master data for slave"],
    }
}

define mysql::datasource($rootpw, $ds_name, $ds_owner, $ds_owner_pwd, $ds_user, $ds_user_pwd, $ds_schema) {
    # TODO: This will soon be replaced.
    # The execs will selectively run based on the value of $mysql_type
    include mysql::standalone

    $mysql_root_cmd = "/usr/bin/mysql -u root -p$rootpw"
    exec { "create datasource $ds_name":
        command     => "/usr/bin/mysqladmin -u root -p$rootpw create $ds_name",
        unless      => "$mysql_root_cmd $ds_name -e '\s'",
        require     => [Service["mysql"], Exec["mysql root password init"]],
    }

    exec { "create grants $ds_name":
        command     => "$mysql_root_cmd -e \"GRANT ALL PRIVILEGES ON *.* TO '$ds_owner'@'%' IDENTIFIED BY '$ds_owner_pwd' WITH GRANT OPTION;\"",
        unless      => "/usr/bin/mysql --host $ipaddress --user=$ds_owner -password=$ds_owner_pwd -e '\s'",
        require     => [Service["mysql"], Exec["mysql root password init"]],
    }

    exec { "create grants $ds_owner":
        command     => "$mysql_root_cmd -e \"GRANT SELECT,INSERT,UPDATE,DELETE ON $ds_name.* TO '$ds_user'@'%' IDENTIFIED BY '$ds_user_pwd';\"",
        unless      => "/usr/bin/mysql --host $ipaddress --user=$ds_user -password=$ds_user_pwd -e '\s'",
        require     => [Service["mysql"], Exec["mysql root password init"]],
    }
    # Only create the schema is a template directory was specified
    if $ds_schema {        
        exec { "create db $ds_name":
            command     => "$mysql_root_cmd $ds_name < $ds_schema > /var/lib/mysql/${ds_name}-create-db.log",
            creates     => "/var/lib/mysql/${ds_name}-create-db.log",
            require     => [Service["mysql"], Exec["create datasource $ds_name"]],
        }
    }
}