register-services.sh 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!/bin/bash
  2. # 手动注册服务到Keystone service catalog
  3. # 用于在没有region服务的情况下注册基础服务
  4. set -e
  5. # 加载数据库配置
  6. if [ -f "../.env.local" ]; then
  7. source ../.env.local
  8. elif [ -f ".env.local" ]; then
  9. source .env.local
  10. fi
  11. DB_HOST=${DB_HOST:-localhost}
  12. DB_PORT=${DB_PORT:-3306}
  13. DB_USER=${DB_USER:-root}
  14. DB_PASSWORD=${DB_PASSWORD}
  15. DB_NAME="yunioncloud"
  16. echo "=== 注册服务到Keystone Service Catalog ==="
  17. # 注册服务和端点
  18. mysql -h $DB_HOST -P $DB_PORT -u $DB_USER -p$DB_PASSWORD $DB_NAME << 'EOF'
  19. -- 清理可能存在的旧数据
  20. DELETE FROM endpoint;
  21. DELETE FROM service;
  22. -- 1. 注册 identity 服务 (keystone)
  23. INSERT INTO service (id, name, type, enabled, created_at, updated_at, update_version, deleted, is_emulated, config_version)
  24. VALUES ('identity-service-id', 'keystone', 'identity', 1, NOW(), NOW(), 0, 0, 0, 0);
  25. INSERT INTO endpoint (id, service_id, region_id, interface, url, enabled, created_at, updated_at, update_version, deleted, name)
  26. VALUES
  27. ('identity-endpoint-public', 'identity-service-id', 'default', 'public', 'http://localhost:35357/v3', 1, NOW(), NOW(), 0, 0, 'keystone'),
  28. ('identity-endpoint-internal', 'identity-service-id', 'default', 'internal', 'http://localhost:35357/v3', 1, NOW(), NOW(), 0, 0, 'keystone'),
  29. ('identity-endpoint-admin', 'identity-service-id', 'default', 'admin', 'http://localhost:35357/v3', 1, NOW(), NOW(), 0, 0, 'keystone');
  30. -- 2. 注册 image 服务 (glance)
  31. INSERT INTO service (id, name, type, enabled, created_at, updated_at, update_version, deleted, is_emulated, config_version)
  32. VALUES ('image-service-id', 'glance', 'image', 1, NOW(), NOW(), 0, 0, 0, 0);
  33. INSERT INTO endpoint (id, service_id, region_id, interface, url, enabled, created_at, updated_at, update_version, deleted, name)
  34. VALUES
  35. ('image-endpoint-public', 'image-service-id', 'default', 'public', 'http://localhost:9292', 1, NOW(), NOW(), 0, 0, 'glance'),
  36. ('image-endpoint-internal', 'image-service-id', 'default', 'internal', 'http://localhost:9292', 1, NOW(), NOW(), 0, 0, 'glance'),
  37. ('image-endpoint-admin', 'image-service-id', 'default', 'admin', 'http://localhost:9292', 1, NOW(), NOW(), 0, 0, 'glance');
  38. -- 3. 注册 yunionconf 服务
  39. INSERT INTO service (id, name, type, enabled, created_at, updated_at, update_version, deleted, is_emulated, config_version)
  40. VALUES ('yunionconf-service-id', 'yunionconf', 'yunionconf', 1, NOW(), NOW(), 0, 0, 0, 0);
  41. INSERT INTO endpoint (id, service_id, region_id, interface, url, enabled, created_at, updated_at, update_version, deleted, name)
  42. VALUES
  43. ('yunionconf-endpoint-public', 'yunionconf-service-id', 'default', 'public', 'http://localhost:30889', 1, NOW(), NOW(), 0, 0, 'yunionconf'),
  44. ('yunionconf-endpoint-internal', 'yunionconf-service-id', 'default', 'internal', 'http://localhost:30889', 1, NOW(), NOW(), 0, 0, 'yunionconf'),
  45. ('yunionconf-endpoint-admin', 'yunionconf-service-id', 'default', 'admin', 'http://localhost:30889', 1, NOW(), NOW(), 0, 0, 'yunionconf');
  46. -- 4. 注册 monitor 服务
  47. INSERT INTO service (id, name, type, enabled, created_at, updated_at, update_version, deleted, is_emulated, config_version)
  48. VALUES ('monitor-service-id', 'monitor', 'monitor', 1, NOW(), NOW(), 0, 0, 0, 0);
  49. INSERT INTO endpoint (id, service_id, region_id, interface, url, enabled, created_at, updated_at, update_version, deleted, name)
  50. VALUES
  51. ('monitor-endpoint-public', 'monitor-service-id', 'default', 'public', 'http://localhost:30093', 1, NOW(), NOW(), 0, 0, 'monitor'),
  52. ('monitor-endpoint-internal', 'monitor-service-id', 'default', 'internal', 'http://localhost:30093', 1, NOW(), NOW(), 0, 0, 'monitor'),
  53. ('monitor-endpoint-admin', 'monitor-service-id', 'default', 'admin', 'http://localhost:30093', 1, NOW(), NOW(), 0, 0, 'monitor');
  54. -- 5. 注册 scheduledtask 服务
  55. INSERT INTO service (id, name, type, enabled, created_at, updated_at, update_version, deleted, is_emulated, config_version)
  56. VALUES ('scheduledtask-service-id', 'scheduledtask', 'scheduledtask', 1, NOW(), NOW(), 0, 0, 0, 0);
  57. INSERT INTO endpoint (id, service_id, region_id, interface, url, enabled, created_at, updated_at, update_version, deleted, name)
  58. VALUES
  59. ('scheduledtask-endpoint-public', 'scheduledtask-service-id', 'default', 'public', 'http://localhost:30891', 1, NOW(), NOW(), 0, 0, 'scheduledtask'),
  60. ('scheduledtask-endpoint-internal', 'scheduledtask-service-id', 'default', 'internal', 'http://localhost:30891', 1, NOW(), NOW(), 0, 0, 'scheduledtask'),
  61. ('scheduledtask-endpoint-admin', 'scheduledtask-service-id', 'default', 'admin', 'http://localhost:30891', 1, NOW(), NOW(), 0, 0, 'scheduledtask');
  62. -- 6. 注册 compute 服务 (region) - 使用compute_v2类型
  63. INSERT INTO service (id, name, type, enabled, created_at, updated_at, update_version, deleted, is_emulated, config_version)
  64. VALUES ('compute-service-id', 'region', 'compute_v2', 1, NOW(), NOW(), 0, 0, 0, 0);
  65. INSERT INTO endpoint (id, service_id, region_id, interface, url, enabled, created_at, updated_at, update_version, deleted, name)
  66. VALUES
  67. ('compute-endpoint-public', 'compute-service-id', 'default', 'public', 'http://localhost:30888', 1, NOW(), NOW(), 0, 0, 'region'),
  68. ('compute-endpoint-internal', 'compute-service-id', 'default', 'internal', 'http://localhost:30888', 1, NOW(), NOW(), 0, 0, 'region'),
  69. ('compute-endpoint-admin', 'compute-service-id', 'default', 'admin', 'http://localhost:30888', 1, NOW(), NOW(), 0, 0, 'region');
  70. -- 7. 注册 yunionagent 服务 (指向yunionconf)
  71. INSERT INTO service (id, name, type, enabled, created_at, updated_at, update_version, deleted, is_emulated, config_version)
  72. VALUES ('yunionagent-service-id', 'yunionagent', 'yunionagent', 1, NOW(), NOW(), 0, 0, 0, 0);
  73. INSERT INTO endpoint (id, service_id, region_id, interface, url, enabled, created_at, updated_at, update_version, deleted, name)
  74. VALUES
  75. ('yunionagent-endpoint-public', 'yunionagent-service-id', 'default', 'public', 'http://localhost:30889', 1, NOW(), NOW(), 0, 0, 'yunionagent'),
  76. ('yunionagent-endpoint-internal', 'yunionagent-service-id', 'default', 'internal', 'http://localhost:30889', 1, NOW(), NOW(), 0, 0, 'yunionagent'),
  77. ('yunionagent-endpoint-admin', 'yunionagent-service-id', 'default', 'admin', 'http://localhost:30889', 1, NOW(), NOW(), 0, 0, 'yunionagent');
  78. -- 8. 注册 victoria-metrics 服务 (时序数据库)
  79. INSERT INTO service (id, name, type, enabled, created_at, updated_at, update_version, deleted, is_emulated, config_version)
  80. VALUES ('victoria-metrics-service-id', 'victoria-metrics', 'victoria-metrics', 1, NOW(), NOW(), 0, 0, 0, 0);
  81. INSERT INTO endpoint (id, service_id, region_id, interface, url, enabled, created_at, updated_at, update_version, deleted, name)
  82. VALUES
  83. ('victoria-metrics-endpoint-public', 'victoria-metrics-service-id', 'default', 'public', 'http://localhost:8428', 1, NOW(), NOW(), 0, 0, 'victoria-metrics'),
  84. ('victoria-metrics-endpoint-internal', 'victoria-metrics-service-id', 'default', 'internal', 'http://localhost:8428', 1, NOW(), NOW(), 0, 0, 'victoria-metrics'),
  85. ('victoria-metrics-endpoint-admin', 'victoria-metrics-service-id', 'default', 'admin', 'http://localhost:8428', 1, NOW(), NOW(), 0, 0, 'victoria-metrics');
  86. -- 9. 注册 region 表(即使region服务未运行,也需要有region记录)
  87. INSERT INTO region (id, name, created_at, updated_at, update_version, deleted)
  88. VALUES ('default', 'default', NOW(), NOW(), 0, 0)
  89. ON DUPLICATE KEY UPDATE updated_at=NOW();
  90. -- 10. 初始化 global-settings 参数(前端需要)
  91. INSERT INTO paramters_tbl (id, name, service, value, created_at, updated_at, update_version, deleted)
  92. VALUES (
  93. REPLACE(UUID(), '-', ''),
  94. 'global-settings',
  95. 'yunionagent',
  96. '{"setupKeys":["zh-CN","en","ja-JP"],"productVersion":"CMP"}',
  97. NOW(),
  98. NOW(),
  99. 0,
  100. 0
  101. )
  102. ON DUPLICATE KEY UPDATE value='{"setupKeys":["zh-CN","en","ja-JP"],"productVersion":"CMP"}', updated_at=NOW();
  103. SELECT 'Services registered successfully' as status;
  104. SELECT COUNT(*) as service_count FROM service;
  105. SELECT COUNT(*) as endpoint_count FROM endpoint;
  106. EOF
  107. echo ""
  108. echo "✓ 服务注册完成"
  109. echo ""
  110. echo "已注册的服务:"
  111. echo " - identity (keystone) - http://localhost:35357/v3"
  112. echo " - compute (region) - http://localhost:30888"
  113. echo " - image (glance) - http://localhost:9292"
  114. echo " - yunionconf - http://localhost:30889"
  115. echo " - yunionagent - http://localhost:30889 (指向yunionconf)"
  116. echo " - monitor - http://localhost:30093"
  117. echo " - scheduledtask - http://localhost:30891"
  118. echo " - victoria-metrics - http://localhost:8428"
  119. echo ""
  120. echo "✓ global-settings 参数已初始化"
  121. echo ""
  122. echo "注意: 需要重启服务以加载新的service catalog"