PageRenderTime 3973ms CodeModel.GetById 38ms RepoModel.GetById 1ms app.codeStats 0ms

/ecs-scheduler-service/src/main/java/com/atlassian/buildeng/ecs/SchedulerApplication.java

https://bitbucket.org/okaner1/per-build-container
Java | 126 lines | 95 code | 10 blank | 21 comment | 7 complexity | 8ceee511c243515b01698a4f89a466c9 MD5 | raw file
  1. /*
  2. * Copyright 2016 - 2017 Atlassian Pty Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.atlassian.buildeng.ecs;
  17. import com.atlassian.buildeng.ecs.scheduling.ECSConfiguration;
  18. import com.atlassian.buildeng.ecs.resources.SchedulerResource;
  19. import com.atlassian.buildeng.ecs.resources.HeartBeatResource;
  20. import com.atlassian.buildeng.ecs.resources.LogsResource;
  21. import com.atlassian.buildeng.ecs.scheduling.AWSSchedulerBackend;
  22. import com.atlassian.buildeng.ecs.scheduling.AwsPullModelLoader;
  23. import com.atlassian.buildeng.ecs.scheduling.CyclingECSScheduler;
  24. import com.atlassian.buildeng.ecs.scheduling.DefaultModelUpdater;
  25. import com.atlassian.buildeng.ecs.scheduling.ECSScheduler;
  26. import com.atlassian.buildeng.ecs.scheduling.ModelLoader;
  27. import com.atlassian.buildeng.ecs.scheduling.ModelUpdater;
  28. import com.atlassian.buildeng.ecs.scheduling.SchedulerBackend;
  29. import com.atlassian.buildeng.ecs.scheduling.TaskDefinitionRegistrations;
  30. import com.atlassian.event.api.EventPublisher;
  31. import com.google.inject.AbstractModule;
  32. import com.google.inject.Guice;
  33. import com.google.inject.Injector;
  34. import com.google.inject.name.Names;
  35. import io.dropwizard.Configuration;
  36. import io.dropwizard.setup.Bootstrap;
  37. import io.dropwizard.setup.Environment;
  38. import java.io.Closeable;
  39. import java.io.IOException;
  40. import java.util.HashMap;
  41. import java.util.Map;
  42. import org.apache.commons.lang3.StringUtils;
  43. import org.eclipse.jetty.util.component.AbstractLifeCycle;
  44. import org.eclipse.jetty.util.component.LifeCycle;
  45. /**
  46. * Created by ojongerius on 09/05/2016.
  47. */
  48. public class SchedulerApplication extends io.dropwizard.Application<Configuration> {
  49. public SchedulerApplication() {
  50. }
  51. public static void main(String[] args) throws Exception {
  52. validateEnvironment();
  53. new SchedulerApplication().run(new String[] {"server"});
  54. }
  55. private static void validateEnvironment() {
  56. if (StringUtils.isBlank(System.getenv(ECSConfigurationImpl.ECS_ASG)) ||
  57. StringUtils.isBlank(System.getenv(ECSConfigurationImpl.ECS_CLUSTER)) ||
  58. StringUtils.isBlank(System.getenv(ECSConfigurationImpl.ECS_TASK_DEF))) {
  59. throw new IllegalStateException("Environment variables " +
  60. ECSConfigurationImpl.ECS_ASG + ", " +
  61. ECSConfigurationImpl.ECS_CLUSTER + ", " +
  62. ECSConfigurationImpl.ECS_TASK_DEF + " are mandatory.");
  63. }
  64. }
  65. @Override
  66. public void initialize(Bootstrap<Configuration> bootstrap) {
  67. // Nothing here yet
  68. }
  69. @Override
  70. public void run(Configuration configuration,
  71. Environment environment) {
  72. Injector injector = Guice.createInjector(new AbstractModule() {
  73. @Override
  74. protected void configure() {
  75. //system environment mapped to @Named, sort of shortcut
  76. Map<String, String> props = new HashMap<>(System.getenv());
  77. if (!props.containsKey(ECSConfigurationImpl.ECS_LOG_DRIVER)) {
  78. props.put(ECSConfigurationImpl.ECS_LOG_DRIVER, "");
  79. }
  80. if (!props.containsKey(ECSConfigurationImpl.ECS_LOG_OPTIONS)) {
  81. props.put(ECSConfigurationImpl.ECS_LOG_OPTIONS, "");
  82. }
  83. String ddApi = System.getenv(DatadogEventPublisher.DATADOG_API);
  84. if (ddApi != null) {
  85. props.put(DatadogEventPublisher.DATADOG_API, ddApi);
  86. bind(EventPublisher.class).to(DatadogEventPublisher.class);
  87. } else {
  88. bind(EventPublisher.class).to(DummyEventPublisher.class);
  89. }
  90. bind(ECSConfiguration.class).to(ECSConfigurationImpl.class);
  91. bind(ECSScheduler.class).to(CyclingECSScheduler.class);
  92. bind(SchedulerBackend.class).to(AWSSchedulerBackend.class);
  93. bind(ModelLoader.class).to(AwsPullModelLoader.class);
  94. bind(ModelUpdater.class).to(DefaultModelUpdater.class);
  95. bind(TaskDefinitionRegistrations.Backend.class).to(ECSConfigurationImpl.class);
  96. Names.bindProperties(binder(), props);
  97. }
  98. });
  99. //make sure to close datadog before stopping.
  100. environment.lifecycle().addLifeCycleListener(new AbstractLifeCycle.AbstractLifeCycleListener() {
  101. @Override
  102. public void lifeCycleStopping(LifeCycle event) {
  103. EventPublisher ep = injector.getInstance(EventPublisher.class);
  104. if (ep instanceof Closeable) {
  105. try {
  106. ((Closeable) ep).close();
  107. } catch (IOException ex) {
  108. }
  109. }
  110. }
  111. });
  112. environment.jersey().register(injector.getInstance(SchedulerResource.class));
  113. environment.jersey().register(injector.getInstance(HeartBeatResource.class));
  114. environment.jersey().register(injector.getInstance(LogsResource.class));
  115. }
  116. }