<commit_before>// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package converter

import (
	"github.com/juju/errors"
	"github.com/juju/loggo"
	"github.com/juju/utils"

	"github.com/juju/juju/agent"
	"github.com/juju/juju/api/converter"
	"github.com/juju/juju/api/watcher"
	"github.com/juju/juju/state/multiwatcher"
	"github.com/juju/juju/worker"
)

var logger = loggo.GetLogger("juju.worker.converter")

// Converter ...
type Converter struct {
	st     *converter.State
	ent    Entity
	config agent.Config
}

type Entity interface {
	SetPassword(string) error
	Jobs() []multiwatcher.MachineJob
}

// NewConverter ...
func NewConverter(
	ent Entity,
	st *converter.State,
	agentConfig agent.Config,
) worker.Worker {
	return worker.NewNotifyWorker(&Converter{
		ent:    ent,
		st:     st,
		config: agentConfig,
	})
}

func (c *Converter) SetUp() (watcher.NotifyWatcher, error) {
	logger.Infof("setting up Converter watcher for %s", c.config.Tag().String())
	return c.st.WatchForJobsChanges(c.config.Tag().String())
}

func (c *Converter) Handle() error {
	logger.Infof("environment for %q has been changed", c.config.Tag())

	jobs, err := c.st.Jobs(c.config.Tag().String())
	if err != nil {
		return errors.Trace(err)
	}

	for _, job := range jobs.Jobs {
		logger.Infof("job found: %q", job)
		logger.Infof("job details: #v", job)
		if job.NeedsState() {
			logger.Infof("converting %q to a state server", c.config.Tag())
			pw, err := utils.RandomPassword()
			if err != nil {
				return errors.Trace(err)
			}
			if err := c.ent.SetPassword(pw); err != nil {
				return errors.Trace(err)
			}
			// change agentConfig too?
			// restart juju
		}
	}
	return nil
}

func (c *Converter) TearDown() error {
	return nil
}<commit_msg>WIP: reboot machine, for now..<commit_after>// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package converter

import (
	"github.com/juju/errors"
	"github.com/juju/loggo"
	"github.com/juju/utils"

	"github.com/juju/juju/agent"
	"github.com/juju/juju/api/converter"
	"github.com/juju/juju/api/watcher"
	"github.com/juju/juju/state/multiwatcher"
	"github.com/juju/juju/worker"
)

var logger = loggo.GetLogger("juju.worker.converter")

// Converter ...
type Converter struct {
	st     *converter.State
	ent    Entity
	config agent.Config
}

type Entity interface {
	SetPassword(string) error
	Jobs() []multiwatcher.MachineJob
}

// NewConverter ...
func NewConverter(
	ent Entity,
	st *converter.State,
	agentConfig agent.Config,
) worker.Worker {
	return worker.NewNotifyWorker(&Converter{
		ent:    ent,
		st:     st,
		config: agentConfig,
	})
}

func (c *Converter) SetUp() (watcher.NotifyWatcher, error) {
	logger.Infof("setting up Converter watcher for %s", c.config.Tag().String())
	return c.st.WatchForJobsChanges(c.config.Tag().String())
}

func (c *Converter) Handle() error {
	logger.Infof("environment for %q has been changed", c.config.Tag())

	jobs, err := c.st.Jobs(c.config.Tag().String())
	if err != nil {
		return errors.Trace(err)
	}

	for _, job := range jobs.Jobs {
		logger.Infof("job found: %q", job)
		logger.Infof("job details: #v", job)
		if job.NeedsState() {
			logger.Infof("converting %q to a state server", c.config.Tag())
			pw, err := utils.RandomPassword()
			if err != nil {
				return errors.Trace(err)
			}
			if err := c.ent.SetPassword(pw); err != nil {
				return errors.Trace(err)
			}
			// change agentConfig too?

			// restart juju, probably a better way?
			return worker.ErrRebootMachine
		}
	}
	return nil
}

func (c *Converter) TearDown() error {
	return nil
}
{'fraction_non_alphanumeric': 0.14911797133406834, 'fraction_numerical': 0.0027563395810363835, 'mean_word_length': 3.6170483460559795, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1439065', 'n_tokens_mistral': 1426, 'n_tokens_neox': 1314, 'n_words': 306}