/ext/QVD-DB/lib/QVD/DB.pm

https://github.com/BillTheBest/theqvd · Perl · 161 lines · 118 code · 39 blank · 4 comment · 5 complexity · 2a7da1dd0faad78243c2e11e91593bb1 MD5 · raw file

  1. package QVD::DB;
  2. our $VERSION = '0.01';
  3. use warnings;
  4. use strict;
  5. use Carp;
  6. use DBIx::Class::Exception;
  7. use Socket qw(IPPROTO_TCP SOL_SOCKET SO_KEEPALIVE);
  8. use Socket::Linux qw(TCP_KEEPIDLE TCP_KEEPINTVL TCP_KEEPCNT);
  9. use QVD::Config::Core;
  10. use parent qw(DBIx::Class::Schema);
  11. __PACKAGE__->load_namespaces(result_namespace => 'Result');
  12. __PACKAGE__->exception_action(sub { croak @_ ; DBIx::Class::Exception::throw(@_);});
  13. my $db_name = core_cfg('database.name');
  14. my $db_user = core_cfg('database.user');
  15. my $db_host = core_cfg('database.host');
  16. my $db_passwd = core_cfg('database.password');
  17. my $db_connect_timeout = core_cfg('internal.database.client.connect.timeout');
  18. my $db_keepidle = core_cfg('internal.database.client.socket.keepidle');
  19. my $db_keepintvl = core_cfg('internal.database.client.socket.keepintvl');
  20. my $db_keepcnt = core_cfg('internal.database.client.socket.keepcnt');
  21. sub new {
  22. my $class = shift;
  23. $class->SUPER::connect("dbi:Pg:dbname=$db_name;host=$db_host;connect_timeout=$db_connect_timeout",
  24. $db_user, $db_passwd,
  25. { RaiseError => 1,
  26. AutoCommit => 1,
  27. quote_char => '"',
  28. name_sep => '.',
  29. on_connect_call => \&_make_pg_socket_keepalive });
  30. }
  31. sub _make_pg_socket_keepalive {
  32. my $storage = shift;
  33. my $dbh = $storage->_dbh // die "not connected to database";
  34. # we have to duplicate the socket as setsockopt does not accept a
  35. # file descriptor
  36. open my $socket, '+<&', $dbh->{pg_socket};
  37. unless (setsockopt($socket, SOL_SOCKET, SO_KEEPALIVE, 1) and
  38. # see tcp(7)
  39. setsockopt($socket, IPPROTO_TCP, TCP_KEEPIDLE, 10) and
  40. setsockopt($socket, IPPROTO_TCP, TCP_KEEPINTVL, 5) and
  41. setsockopt($socket, IPPROTO_TCP, TCP_KEEPCNT, 3)) {
  42. die "Unable to set database client socket keepalive options: $!";
  43. }
  44. }
  45. my %initial_values = ( VM_State => [qw(stopped
  46. starting
  47. running
  48. stopping
  49. zombie
  50. debugging )],
  51. VM_Cmd => [qw(start stop busy)],
  52. User_State => [qw(disconnected connecting connected)],
  53. User_Cmd => [qw(abort)],
  54. Host_State => [qw(stopped starting running stopping lost)],
  55. Host_Cmd => [qw(stop)] );
  56. sub deploy {
  57. my $db = shift;
  58. # Ensure the default transaction isolation is "serializable" (see #1210)
  59. my $dbh = $db->storage->dbh;
  60. $dbh->do("ALTER DATABASE $db_name SET default_transaction_isolation TO serializable");
  61. $db->SUPER::deploy(@_);
  62. while (my ($rs, $names) = each %initial_values) {
  63. $db->resultset($rs)->create({name => $_}) for @$names;
  64. }
  65. $db->resultset('Version')->create({ component => 'schema',
  66. version => '3.3.0' });
  67. }
  68. sub erase {
  69. my $db = shift;
  70. my $dbh = $db->storage->dbh;
  71. for my $table (qw( versions
  72. vm_runtimes
  73. vms
  74. vm_properties
  75. osfs
  76. host_runtimes
  77. hosts
  78. host_properties
  79. dis
  80. di_tags
  81. users
  82. user_properties
  83. vm_states
  84. user_states
  85. vm_cmds
  86. user_cmds
  87. configs
  88. ssl_configs )
  89. ) {
  90. eval {
  91. warn "DROPPING $table\n";
  92. $dbh->do("DROP TABLE $table CASCADE");
  93. };
  94. warn "Error (DROP $table): $@" if $@;
  95. }
  96. }
  97. 1;
  98. __END__
  99. =head1 NAME
  100. QVD::DB - ORM for QVD entities
  101. =head1 SYNOPSIS
  102. use QVD::DB;
  103. my $foo = QVD::DB->new();
  104. ...
  105. =head1 DESCRIPTION
  106. =head2 API
  107. =over 4
  108. =item $db = QVD::DB->new()
  109. Opens a new connection to the database using the configuration from
  110. the file 'config.ini'
  111. =item $db->erase()
  112. Drops all the database tables.
  113. =back
  114. =head1 AUTHORS
  115. Joni Salonen (jsalonen at qindel.es)
  116. Nicolas Arenas (narenas at qindel.es)
  117. Salvador FandiE<ntilde>o (sfandino@yahoo.com)
  118. =head1 COPYRIGHT
  119. Copyright 2009-2010 by Qindel Formacion y Servicios S.L.
  120. This program is free software; you can redistribute it and/or modify it
  121. under the terms of the GNU GPL version 3 as published by the Free
  122. Software Foundation.