#!/usr/bin/env python3
# Activate changes made via the omdclient scripts.

#########################################################################
### Declarations ########################################################
#########################################################################

import omdclient, os, sys

#########################################################################
### Configuration #######################################################
#########################################################################
## Managed via central libraries and /etc/omd_client

config_file_base = '/etc/omdclient/config.yaml'

## Text for --help
text = "Activate changes in check_mk/WATO interface"
usage_text = "usage: %prog [options]"

#########################################################################
### main () #############################################################
#########################################################################

def main():
    config_file = os.environ.get('OMDCONFIG', config_file_base)
    try:
        config = omdclient.loadCfg(config_file)
    except Exception as e:
        print("failed to load config: %s" % (e))
        sys.exit(3)

    p = omdclient.generateParser(text, usage_text, config)
    p.add_option('--foreign_ok', dest='foreign_ok',
        action="store_true", default=True,
        help="apply foreign commits? (default: %default)")
    opt, args = p.parse_args()
    argdict = omdclient.parserArgDict(opt)
    argdict['foreign_ok'] = opt.foreign_ok

    try:
        value, result = omdclient.activateChanges(argdict)
        if not value:
            if result is None: print("unknown error")
            else: print(result)
            sys.exit(1)
        else:
            print(result)
            sys.exit(0)
    except Exception as e:
        print("failed to activate changes: %s" % (e))
        sys.exit(-1)

if __name__ == "__main__":
    main()

#########################################################################
### POD Documentation ###################################################
#########################################################################

"""

=head1 NAME

omd-activate - activate changes on the OMD site

=head1 SYNOPSIS

B<omd-activate>

=head1 USAGE

omd-activate simply activates the changes that were made in other scripts.
It generally takes a long time to run, and (sometimes) get back a 503 http
error because of how long it takes.

=head1 ARGUMENTS

=over 4

=item B<--debug>

If set, print debugging information.

=item B<--apikey> I<key>

Password for the API User.  Default: comes from the configuration file.

=item B<--server> I<server>

Host name of the server.  Default: comes from the configuration file.

=item B<--site> I<site>

Site name within the server.  Default: comes from the configuration file.

=item B<--user> I<user>

API User name.  The user must exist on the server, and be an 'automation
user'.  Default: comes from the configuration file.

=back

=head1 FILES

=over 4

=item F</etc/omdclient/config.yaml>

=back

=head1 SEE ALSO

https://mathias-kettner.de/checkmk_wato_webapi.html

=head1 AUTHOR

Tim Skirvin <tskirvin@fnal.gov>

=head1 LICENSE + COPYRIGHT

Copyright 2015, Fermi National Accelerator Laboratory

This program is free software; you may redistribute it and/or modify it
under the same terms as Perl itself.

=cut

"""
